Come interrogare un custom post type per termine?

3 mag 2012, 16:11:41
Visualizzazioni: 15.3K
Voti: 2

Sembra banale ma non riesco a farlo funzionare. Ecco il mio codice:

        $args = array(
            'numberposts'     => -1,
            'eventcategory'   => 'nice-events',
            'post_type'       => 'event',
            'post_status'     => 'publish' 
        );

        var_dump(get_posts($args));

Come puoi vedere, la mia tassonomia è eventcategory e il termine è nice-events. Ci sono effettivamente oltre 50 termini e indipendentemente da quale termine utilizzo al posto di nice-events ottengo sempre lo stesso risultato: tutti i post. Quindi il termine viene ignorato e non ho idea del perché.

0
Tutte le risposte alla domanda 1
6
10

Non puoi inventarti i tuoi argomenti - invece di sostituire l'argomento 'category' con il nome della tua tassonomia, usa 'tax_query'.

Vedi la sezione "parametri della tassonomia" nel codex su get_posts.

$args = array(
    'post_type' => 'event',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'eventcategory',
            'field' => 'slug',
            'terms' => 'nice-events',
        ),
    ),
);


$your_query = get_posts( $args );

// fai qualcosa con $your_query

In alternativa, potresti utilizzare la classe WP_Query:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'event',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'eventcategory',
            'field' => 'slug',
            'terms' => 'nice-events'
        ),
    ),
);


$your_query = new WP_Query( $args );

// fai qualcosa con $your_query
3 mag 2012 16:36:41
Commenti

@kaiser In effetti, puoi. Ero convinto che get_posts() non supportasse tax_queries. Hai ragione, il codex dice che lo fa.

Johannes Pille Johannes Pille
3 mag 2012 17:24:24

Non ottengo alcun risultato usando il tuo codice..

jilseego jilseego
3 mag 2012 17:36:21

@jilseego C'era una virgola fuori posto - ora lo snippet dovrebbe essere pronto per il copia & incolla...

Johannes Pille Johannes Pille
3 mag 2012 17:40:24

Sì, l'ho già corretto prima che tu lo facessi. Ma non ho ottenuto alcun risultato, davvero.

jilseego jilseego
3 mag 2012 17:42:51

Il primo restituisce un array vuoto. Il secondo restituisce questo: http://pastebin.com/wnGE88Cn, che non credo sia corretto(?).

jilseego jilseego
3 mag 2012 17:56:24

Quindi usando WP_Query() ottengo ancora un risultato vuoto perché $your_query->have_posts() restituisce un booleano false.

jilseego jilseego
3 mag 2012 18:05:29
Mostra i restanti 1 commenti