Come usare wp_get_object_terms() per ottenere tutti i termini associati ai post nella query corrente
Come posso usare wp_get_object_terms() per ottenere un elenco di tutti i termini associati a tutti i post nella query corrente?
Ad esempio, per la query corrente voglio ottenere un array dei termini dalla tassonomia "Alfa" che sono contenuti in tutti i post interrogati.
wp_get_object_terms($wp_query, 'alfa');
Ma sembra che questo restituisca solo un elemento nell'array...
Sto facendo questo per costruire un array per verificare incroci tra una tassonomia e un'altra per un menu di navigazione, e attualmente lo sto facendo con il seguente codice ma penso che ci debba essere un modo migliore.
Per favore aiutatemi! Grazie!
$queried_terms = array();
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$postid = $post->ID;
if( has_term( '', 'alfa', $postid) ) {
$terms = get_the_terms( $postid, 'alfa' );
foreach($terms as $term) {
$queried_terms[] = $term->slug;
}
}
endwhile; endif;
rewind_posts();
wp_reset_query();
$queried_terms = array_unique($queried_terms);

Penso che tu sia sulla strada giusta perché wp_get_object_terms()
può accettare un array di ID come primo argomento, è solo che $wp_query non è l'array che desideri.
Non posso garantire che questo sia più efficiente (non è la mia area di competenza), ma credo che questo snippet [parzialmente testato] farebbe ciò che vuoi con almeno un ciclo in meno e senza array_unique()
:
// ottieni $wp_query
global $wp_query;
// ottieni array di oggetti post
$my_posts = $wp_query -> posts;
// crea array per gli ID dei post
$my_post_ids = array();
// cicla attraverso l'array dei post per gli ID
foreach( $my_posts as $my_post ) {
$my_post_ids[] = $my_post->ID;
}
// ottieni i termini
$my_terms = wp_get_object_terms( $my_post_ids, 'alfa' );
wp_get_object_terms()
accetta un 3° parametro $args
che potresti dover impostare per ottenere l'output desiderato, ma lascio a te questa scelta.
AGGIORNAMENTO:
Questo può essere ancora più breve utilizzando la funzione per me nuova wp_list_pluck()
. Anche questo è non testato ma sembra corretto:
// ottieni $wp_query
global $wp_query;
// ottieni array di oggetti post
$my_posts = $wp_query -> posts;
// NUOVO: crea array degli ID dei post in un solo passaggio
$my_post_ids = wp_list_pluck( $my_posts, 'ID' );
// ottieni i termini
$my_terms = wp_get_object_terms( $my_post_ids, 'alfa' );
Puoi vedere nel sorgente che esegue lo stesso codice dei cicli foreach
, ma sembra un po' più pulito.

Questa generalizzazione di quanto sopra ha funzionato per me:
$args = array( 'cat' = -1 ); // ad esempio per ottenere l'elenco dei post in qualsiasi categoria
$postobjs = get_posts( $args );
$postids = wp_list_pluck( $postobjs, 'ID' );
$taxonomy = 'mytax' // il nome della tua tassonomia
$termobjs = wp_get_object_terms( $postids, $taxonomy );
$termlist = array_unique( wp_list_pluck( $termobjs, 'name' ) ); // nomi dei termini distinti
Restituisce un elenco univoco di termini nella tassonomia personalizzata 'mytax'. Grazie @mrwweb :-)
