Ottenere i post assegnati a uno specifico termine di tassonomia personalizzata, escludendo i termini figli
Supponiamo di avere i seguenti termini di tassonomia:
Termine 1
Termine 1.1
Termine 1.2
Termine 2
Termine 2.1
Come posso ottenere solo i post assegnati al Termine 1 senza includere quelli assegnati al Termine 1.1 o Termine 1.2?
Per esempio:
$pages = get_posts(array(
'post_type' => 'page',
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'id',
'terms' => 1 // Dove term_id del Termine 1 è "1".
)
)
);
mi sta restituendo anche i post che hanno assegnati i Termini 1.1 e 1.2.
Grazie.

Esaminando la classe WP_Tax_Query in /wp-includes/taxonomy.php, ho scoperto che esiste un'opzione 'include_children' che di default è impostata su true. Ho modificato la mia chiamata get_posts() originale con il seguente codice, e funziona perfettamente:
$pages = get_posts(array(
'post_type' => 'page',
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'term_id',
'terms' => 1, /// Dove term_id del Termine 1 è "1".
'include_children' => false
)
)
));
Lista di ulteriori parametri per la query: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

Leggendo dalla pagina del Codex collegata, penso che il valore di 'field' nell'array tax_query dovrebbe essere 'term_id' invece di 'id': "I valori possibili sono 'term_id', 'name' e 'slug'. Il valore predefinito è 'term_id'." Immagino che 'id' funzioni solo perché causa un fallback al valore predefinito.

mi sono imbattuto in questo l'altro giorno:
$tax = 'music';
$oterm = 'pop';
$term = get_term_by('slug', $oterm, $tax);
$termChildren = get_term_children($term->term_id, $tax);
$wp_query = new WP_Query();
$wp_query->query(
array(
'posts_per_page' => '5',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $oterm
),
array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $termChildren,
'operator' => 'NOT IN'
)
)
)
);

Ecco il codice completo, spero ti sia utile. Grazie
<?php
$terms_array = array(
'taxonomy' => 'services', // puoi modificarlo in base alla tua tassonomia
'parent' => 0 // Se parent => 0 viene passato, verranno restituiti solo i termini di primo livello
);
$services_terms = get_terms($terms_array);
foreach($services_terms as $service): ?>
<h4><?php echo $service->name; ?></h4>
<?php
$post_args = array(
'posts_per_page' => -1,
'post_type' => 'service', // puoi modificarlo in base al tuo custom post type
'tax_query' => array(
array(
'taxonomy' => 'services', // puoi modificarlo in base alla tua tassonomia
'field' => 'term_id', // può essere 'term_id', 'slug' o 'name'
'terms' => $service->term_id,
)
)
);
$myposts = get_posts($post_args); ?>
<ul>
<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; // Fine foreach dei Post del Termine ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endforeach; // Fine foreach dei Termini; ?>
