0

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^)

  • 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 Aug 18 '15 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 Aug 18 '15 at 12:54
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,
        ),
    )

);
  • Thanks @vancoder, your code seems like it should work but I still get all posts for both headings (i.e. both times through the outer loop). Here's what I get. I noticed this post which seemed to cover similar territory but even using that method, I get the same result. You can see on my page that the outer loop is returning the desired category ID (I echo it right under its name); I'm not clear on why I can't pass that to the wp_query... – Adam Abrams Aug 18 '15 at 2:27
  • Further bafflement: even when I try and hard-code a single term ID like this: 'terms' => 16,, I still get ALL the posts. – Adam Abrams Aug 18 '15 at 2:39
  • The field key is missing from the array, – Den Isahac Apr 12 '18 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.