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
  • Get all your posts in one query, then sort the results with 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
  • Thanks @PieterGoosen for this - I (think?) I see now that my second query is unrelated to the first one and that's why I'm not getting any filtered results from it. I've learned much now simply by researching the usort function, but this approach is at the outer edges of my PHP ability. This blog post seems to be speaking to almost the same thing - is that right? Even if so, I can't figure out what to use of his code. I guess my core question is, what would the code to sort my query by category look like? – Adam Abrams Commented Aug 18, 2015 at 12:54
Add a comment  | 

1 Answer 1

Reset to default 0

You 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