分配给特定自定义分类术语的get_posts,而不是该术语的孩子
4 个回答
- 投票数
-
- 2011-08-25
在/wp-includes/taxonomy.php中查看WP_Tax_Query类时,我发现有一个'include_children'选项,默认为true.我使用以下命令修改了原始的get_posts()调用,效果很好:
$pages = get_posts(array( 'post_type' => 'page', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => 'taxonomy-name', 'field' => 'term_id', 'terms' => 1, /// Where term_id of Term 1 is "1". 'include_children' => false ) ) ));
更多查询参数列表: http://codex.wordpress.org/Class_Reference/WP_Query#分类标准参数
In looking at the WP_Tax_Query class in /wp-includes/taxonomy.php, I found that there is a 'include_children' option which defaults to true. I modified my original get_posts() call with the following, and it works great:
$pages = get_posts(array( 'post_type' => 'page', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => 'taxonomy-name', 'field' => 'term_id', 'terms' => 1, /// Where term_id of Term 1 is "1". 'include_children' => false ) ) ));
List of more query parameters: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
-
从链接到的法典页面上读取,我认为tax_query数组中"字段"的值应为"term_id"而不是"id":"可能的值为"term_id","name"和" slug".默认值为'term_id'."我猜'id'只起作用,因为它会导致回退到默认值.Reading from the Codex page linked to, I think the value of 'field' in the tax_query array should be 'term_id' rather than 'id': "Possible values are 'term_id', 'name' and 'slug'. Default value is 'term_id'." I'm guessing 'id' only works because it causes a fall-back to the default.
- 3
- 2015-09-26
- Jani Uusitalo
-
- 2011-08-24
前几天碰到过
$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' ) ) ) );
just came across this the other day:
$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' ) ) ) );
-
- 2018-07-14
以下是完整的代码,希望对您有所帮助.谢谢
<?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; ?>
Here is complete code hope it helps. Thanks
<?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; ?>
-
说我有以下分类法术语:
我如何只获得分配给第1条款的帖子,而不包括分配给第1.1或1.2条款的帖子?
例如:
也给我分配了条款1.1和1.2的帖子.
谢谢.