admin管理员组文章数量:1122832
I'm querying a custom post type ("donor") and would like to list them grouped by category (a custom taxonomy for this post type), with the category title then all posts for that category.
After much research, I've got this (in my custom post type's archive template, archive-donor.php):
global $paged;
global $query_args;
$category_args = array(
'taxonomy' => 'donor_taxonomy',
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $category_args );
foreach ( $categories as $category ) {
echo '<h3><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></h3>';
echo 'ID is: ' . $category->term_id . '. ';
$post_args = array(
'cat' => $category->term_id,
'post_type' => 'donor',
'orderby' => 'post_date',
'posts_per_page'=> '-1', // overrides posts per page in theme settings
);
$loop = new WP_Query( $post_args );
if( $loop->have_posts() ) {
[query stuff here that works fine when I do NOT try to specify the category in my query args]
}
wp_reset_postdata();
}
Even though I can echo the ID number with this line...
echo 'ID is: ' . $category->term_id . '. ';
...If I include the following with my $post_args, which uses the same code to extract the ID, it says no posts were found:
'cat' => $category->term_id
If I leave that line out, I get all posts regardless of category, listed after each category title. If I include it, I get "no posts found" after each title. The titles themselves display correctly in order.
I must be doing something wrong but can't for the life of me figure out what. Hope someone can help! 8^)
I'm querying a custom post type ("donor") and would like to list them grouped by category (a custom taxonomy for this post type), with the category title then all posts for that category.
After much research, I've got this (in my custom post type's archive template, archive-donor.php):
global $paged;
global $query_args;
$category_args = array(
'taxonomy' => 'donor_taxonomy',
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $category_args );
foreach ( $categories as $category ) {
echo '<h3><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></h3>';
echo 'ID is: ' . $category->term_id . '. ';
$post_args = array(
'cat' => $category->term_id,
'post_type' => 'donor',
'orderby' => 'post_date',
'posts_per_page'=> '-1', // overrides posts per page in theme settings
);
$loop = new WP_Query( $post_args );
if( $loop->have_posts() ) {
[query stuff here that works fine when I do NOT try to specify the category in my query args]
}
wp_reset_postdata();
}
Even though I can echo the ID number with this line...
echo 'ID is: ' . $category->term_id . '. ';
...If I include the following with my $post_args, which uses the same code to extract the ID, it says no posts were found:
'cat' => $category->term_id
If I leave that line out, I get all posts regardless of category, listed after each category title. If I include it, I get "no posts found" after each title. The titles themselves display correctly in order.
I must be doing something wrong but can't for the life of me figure out what. Hope someone can help! 8^)
Share Improve this question edited Aug 17, 2015 at 21:44 Adam Abrams asked Aug 17, 2015 at 21:38 Adam AbramsAdam Abrams 151 silver badge9 bronze badges 2 |1 Answer
Reset to default 0You mentioned you are using a custom taxonomy - therefore including the cat
key in your wp_query args won't work. wp_query expects that to be the ID of a term belonging to the standard WP category taxonomy. With a custom tax you need to use the dedicated taxonomy parameters.
So your args should be something like:
$post_args = array(
'post_type' => 'donor',
'orderby' => 'post_date',
'posts_per_page'=> '-1', // overrides posts per page in theme settings
'tax_query' => array(
array(
'taxonomy' => 'donor_taxonomy',
'field' => 'term_id',
'terms' => $category->term_id,
),
)
);
本文标签: custom post typesCan39t seem to filter wpquery by current category ID
版权声明:本文标题:custom post types - Can't seem to filter wp_query by current category ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287955a1927991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
usort
. Inside the loop, to display term name, simply check the current post term with the previous post term, if they are the same, display nothing, else display the term name :-) – Pieter Goosen Commented Aug 18, 2015 at 6:26