Come interrogare un custom post type per termine?
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é.
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

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

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

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

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