カスタム分類法を使用してカスタム投稿タイプをクエリするにはどうすればよいですか?
5 回答
- 投票
-
- 2013-02-07
すべてのファーは
query_posts()
を使用しないでください.詳細についてはいつWP_Query、query_posts()、get_posts()を使用する必要がありますか?.p> 必要な投稿を取得するには、
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は、分類クエリの配列とともに使用されます.私はFAQカスタム投稿タイプに取り組んでいましたが、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)でカスタム投稿(FAQ投稿)を取得しています.WP_Query引数の{taxonomy}パラメータはv.3.1以降非推奨になり、{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」と複数の分類法用語「directorate」および「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
-
はい、taxからtay_queryに変更されました.Yes now changed from tax to tay_query..
- 0
- 2019-12-11
- mamunuzaman
何らかの理由で、カスタム分類法を使用して投稿を取得するのに苦労しています...誰かが私の愚かさを解明できますか?
分類宣言:
カスタム投稿タイプ宣言:
}