如何使用自定义分类法查询自定义帖子类型?
5 个回答
- 投票数
-
- 2013-02-07
所有的人永远不要使用
query_posts()
,请在此处详细了解:何时应使用WP_Query vs query_posts()vsget_posts()?. >您必须使用
WP_Query
来获取所需的帖子.阅读文档.在您的情况下,查询可能是这样的:$the_query = new WP_Query( array( 'post_type' => 'Adverts', 'tax_query' => array( array ( 'taxonomy' => 'advert_tag', 'field' => 'slug', 'terms' => 'politics', ) ), ) ); while ( $the_query->have_posts() ) : $the_query->the_post(); // Show Posts ... endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata();
Firs of all don't use
query_posts()
ever, read more about it here: When should you use WP_Query vs query_posts() vs get_posts()?.You have to use
WP_Query
to fetch posts what you need. Read documentation for it. In your case the query could be like this:$the_query = new WP_Query( array( 'post_type' => 'Adverts', 'tax_query' => array( array ( 'taxonomy' => 'advert_tag', 'field' => 'slug', 'terms' => 'politics', ) ), ) ); while ( $the_query->have_posts() ) : $the_query->the_post(); // Show Posts ... endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata();
-
只是注意到,它似乎拉出了所有自定义帖子类型为"广告"的帖子.但是,这似乎可以完成工作: $the_query=new WP_Query(array( 'post_type'=>'广告', 'advert_tag'=>'政治' ));Just noticed that it seems to pull all posts with the custom post type 'Adverts'. However this seems to do the job: $the_query = new WP_Query( array( 'post_type' => 'Adverts', 'advert_tag' => 'politics' ));
- 2
- 2013-02-07
- Stephen
-
@Stephen {tax}自3.1版起已被弃用,而引入了{tax_query}和{tax_query}.这仍然有效,但我们不应该使用不推荐使用的功能. tax_query与一组分类学查询一起使用.我正在使用FAQs Custom Post类型,它对我的工作方式与WP_Query中的{tax}分类标准子句参数几乎相同.@Stephen {tax} was deprecated since version 3.1 in favor of {tax_query} and {tax_query} was introduced. this still works but we should not use the deprecated functions. tax_query is used with an array of taxonomy queries. i was working on FAQs Custom Post type and it worked for me pretty much the same way as {tax} taxonomy slug argument in WP_Query does.
- 0
- 2015-11-19
- Aamer Shahzad
-
- 2015-11-19
我正在使用此查询来获取具有自定义分类法(faq_category)的自定义帖子(常见问题解答帖子).因为自v.3.1起不推荐使用WP_Query args中的{taxonomy}参数,并引入了{tax_query}.下面是完美的代码.
$query = new WP_Query( array( 'post_type' => 'faqs', // name of post type. 'tax_query' => array( array( 'taxonomy' => 'faq_category', // taxonomy name 'field' => 'term_id', // term_id, slug or name 'terms' => 48, // term id, term slug or term name ) ) ) ); while ( $query->have_posts() ) : $query->the_post(); // do stuff here.... endwhile; /** * reset the orignal query * we should use this to reset wp_query */ wp_reset_query();
i am using this query to fetch custom posts (FAQs Posts) with its custom taxonomy (faq_category). since {taxonomy} parameter in WP_Query args was deprecated since v.3.1 and introduced {tax_query}. below is the code that works perfectly.
$query = new WP_Query( array( 'post_type' => 'faqs', // name of post type. 'tax_query' => array( array( 'taxonomy' => 'faq_category', // taxonomy name 'field' => 'term_id', // term_id, slug or name 'terms' => 48, // term id, term slug or term name ) ) ) ); while ( $query->have_posts() ) : $query->the_post(); // do stuff here.... endwhile; /** * reset the orignal query * we should use this to reset wp_query */ wp_reset_query();
-
这是正确的答案-接受的答案将不会按分类法进行过滤,因为tax_query需要数组数组.此嵌套方法对于使其正常工作至关重要.感谢您的回答 )This is the correct answer - the accepted answer will not filter by taxonomy since the tax_query requires an array of arrays. This nested method is essential to getting this to work. Thanks for your answer )
- 0
- 2016-07-28
- Tom Dyer
-
是的,您是对的,欢迎汤姆·代尔yes you are right, welcome Tom Dyer
- 0
- 2016-08-18
- Aamer Shahzad
-
是的,这也帮助我使分类模板工作.谢谢!Yes, this one also helped me to get the taxonomies template to work. Thank you!
- 0
- 2018-02-16
- user3135691
-
嘿@AamerShahzad我有完全相同的问题,我用了您的答案,但是页面没有任何文章.你能帮我吗?https://stackoverflow.com/questions/55783769/how-do-i-pull-only-one-category-from-custom-post-type-in-a-templateHey @AamerShahzad I have the exact same question and I used your answer but the page is pulling no posts. Can you help me out here? https://stackoverflow.com/questions/55783769/how-do-i-pull-only-one-category-from-custom-post-type-in-a-template
- 0
- 2019-04-21
- Desi
-
- 2020-04-28
这里的代码就像魔术一样工作.我正在获取具有自定义分类法" unit_type"和多个分类法术语的"postorate"和" office"的自定义post_type" university_unit"的所有帖子. 希望对您有所帮助.
<?php $args = array( 'post_type' => 'university_unit', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'unit_type', 'field' => 'slug', 'terms' => array('directorate', 'office') ) ) ); $Query = new WP_Query($args); if($Query -> have_posts()): while($Query -> have_posts()): $Query -> the_post(); ?> <div class="cm-post-list-item"> <article> <div class="cm-post-head"> <h3 class="cm-text-blue"> <a href="<?php the_permalink(); ?>"><?php the_title();?></a> </h3> </div> <div class="cm-post-body"><?php the_excerpt();?></div> </article> </div> <?php endwhile; else: "No Administrative Offices Found. Try again later"; endif; wp_reset_postdata(); ?>
Here the code that works like magic. I am fetching all posts for the custom post_type "university_unit" with custom taxonomy "unit_type" and multiple taxonomy terms as "directorate" and "office". I hope it helps out.
<?php $args = array( 'post_type' => 'university_unit', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'unit_type', 'field' => 'slug', 'terms' => array('directorate', 'office') ) ) ); $Query = new WP_Query($args); if($Query -> have_posts()): while($Query -> have_posts()): $Query -> the_post(); ?> <div class="cm-post-list-item"> <article> <div class="cm-post-head"> <h3 class="cm-text-blue"> <a href="<?php the_permalink(); ?>"><?php the_title();?></a> </h3> </div> <div class="cm-post-body"><?php the_excerpt();?></div> </article> </div> <?php endwhile; else: "No Administrative Offices Found. Try again later"; endif; wp_reset_postdata(); ?>
-
- 2020-06-14
这帮助我获得了每个术语下针对CPT的自定义分类法列出的所有帖子
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
This helped me to get all posts listed under each term for custom taxanomy for CPT
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
-
- 2018-09-26
此答案现在不再有效,因为wordpress更改了其分类参数信息.请使用这种方式.会的.这个对我有用."tax_query"替换为"tax".希望它能工作.
$the_query = new WP_Query( array( 'post_type' => 'Adverts', 'tax' => array( array ( 'taxonomy' => 'advert_tag', 'field' => 'slug', 'terms' => 'politics', ) ), ) ); while ( $the_query->have_posts() ) : $the_query->the_post(); // Show Posts ... endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata();
This answer now is not valid anymore since wordpress changes their taxonomy parameter information. please use this way. It will work. It works for me. "tax_query" replaces with "tax". hope it will work.
$the_query = new WP_Query( array( 'post_type' => 'Adverts', 'tax' => array( array ( 'taxonomy' => 'advert_tag', 'field' => 'slug', 'terms' => 'politics', ) ), ) ); while ( $the_query->have_posts() ) : $the_query->the_post(); // Show Posts ... endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata();
-
与之完全相反-"tax"是旧方法,"tax_query"是当前(v3.1 +)方法.It's the exact opposite - `tax` was the old way, `tax_query` is the current (v3.1+) way.
- 0
- 2019-01-03
- WebElaine
-
好吧,我正在使用v4.5,它可以与我一起使用Well I am working v4.5 and it works with me
- 0
- 2019-01-05
- mamunuzaman
-
WP以向后兼容而闻名.旧的方法仍然有效,但是已经过时了,因此最终可能会删除它,使用新方法更安全.WP is famous for being backwards-compatible. The old way still works, but it's been deprecated, so it may eventually be removed and it's safer to use the newer method.
- 0
- 2019-01-07
- WebElaine
-
是,现在从税收更改为tay_query.Yes now changed from tax to tay_query..
- 0
- 2019-12-11
- mamunuzaman
由于某种原因,我发现使用自定义分类法来抓取任何帖子都非常困难...任何人都可以弄明白我的愚蠢吗?
生物分类宣言:
自定义帖子类型声明:
}