Ottenere i post assegnati a uno specifico termine di tassonomia personalizzata, escludendo i termini figli

24 ago 2011, 19:03:49
Visualizzazioni: 153K
Voti: 28

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.

0
Tutte le risposte alla domanda 5
1
64

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

25 ago 2011 23:48:43
Commenti

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.

Jani Uusitalo Jani Uusitalo
26 set 2015 12:22:09
0

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'
            )
        )
    )
);

fonte: http://return-true.com/2011/08/wordpress-display-posts-from-a-term-without-displaying-posts-from-child-terms/

24 ago 2011 19:53:46
0

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; ?>  
14 lug 2018 20:30:32
0

viene utilizzato l'operatore 'IN' e funziona

'taxonomy' => 'collections', 'terms' => array( 28 ), 'field' => 'id', 'operator' => 'IN'

24 ago 2011 19:43:34
0

Puoi evitare la query fiscale in questo modo:

$posts = get_posts(
      array(
          'posts_per_page' => -1,
          'post_type' => 'my_post_type',
          'services' => term->name
      )
);
11 set 2024 15:37:18