Angenommen, ich habe die folgenden Taxonomiebegriffe:
Term 1
Term 1.1
Term 1.2
Term 2
Term 2.1
Wie kann ich nur Posts abrufen, die Term 1 zugeordnet sind und keine Posts enthalten, die Term 1.1 oder Term 1.2 zugeordnet sind?
Zum Beispiel:
$pages = get_posts(array(
'post_type' => 'page',
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'id',
'terms' => 1 // Where term_id of Term 1 is "1".
)
)
);
gibt mir auch Beiträge, denen die Begriffe 1.1 und 1.2 zugewiesen sind.
Vielen Dank.
Beim Betrachten der WP_Tax_Query-Klasse in /wp-includes/taxonomy.php habe ich festgestellt, dass es eine Option 'include_children' gibt, die standardmäßig true ist. Ich habe meinen ursprünglichen get_posts () -Aufruf folgendermaßen geändert, und er funktioniert hervorragend:
$pages = get_posts(array(
'post_type' => 'page',
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'id',
'terms' => 1, // Where term_id of Term 1 is "1".
'include_children' => false
)
)
));
Liste weiterer Abfrageparameter: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
bin neulich auf folgendes gestoßen:
$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'
)
)
)
);
Hier ist vollständiger Code, hoffe, es hilft. Vielen Dank
<?php
$terms_array = array(
'taxonomy' => 'services', // you can change it according to your taxonomy
'parent' => 0 // If parent => 0 is passed, only top-level terms will be returned
);
$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', // you can change it according to your custom post type
'tax_query' => array(
array(
'taxonomy' => 'services', // you can change it according to your taxonomy
'field' => 'term_id', // this can be 'term_id', 'slug' & '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; // Term Post foreach ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endforeach; // End Term foreach; ?>
wird der Operator 'IN' verwendet und es funktioniert
'taxonomy' => 'collections', 'terms' => array (28), 'field' => 'id', 'operator' => 'IN'