Is it possible to create a special wp query that looks to see how many posts exist in a specific taxonomy, or taxonomy term, and executes a particular loop depending on the volume of posts?

My idea behind this is -
Id like to be able to show particular CPT content in a different styled format depending on how many of those posts exist.

For example:
Lets say the taxonomy "product categories" only contains about 2 posts listed with child terms that exist in that taxonomy rather then something like 20...
Well what Id like to do is list those products in a larger style via CSS instead of having to default on the more consolidated styling for when their are a larger volume of products.

Can this be done, and if so how?

1 Answer 1


Just check the total number of posts found:

if($your_query->found_posts > 20) ...

and decide how do you want your mark-up to be.

If want to see how many products are associated with a certain product_category term, use get_term() and check out the 'count' property

  • Hey thank you for the response, could you possibly extrapolate on that a bit with a more detailed example? Id greatly appreciate it!
    – Mr.Brown
    Commented Sep 29, 2011 at 0:59
  • Would this be before the loop or after the loop has begun?
    – Mr.Brown
    Commented Sep 29, 2011 at 2:44
  • I'm assuming you need it before the loop. Is this the main query, or a custom query? You should post the entire code related to your question Commented Sep 29, 2011 at 2:48
  • It needs to be achieved via WP's taxonomy templates if thats possible but I wasn't sure of that being a possibility yet hence my post, so ive still yet to write the code. (or at least I believe what Im trying to achieve can only work with those particular templates??....if this can actually work with a custom page template instead I was unaware of that since I need to be able to view CPT's sorted by custom tax terms)
    – Mr.Brown
    Commented Sep 29, 2011 at 3:52
  • If it will help, I can mock something up that will at least show you what Im trying to do, just let me know.
    – Mr.Brown
    Commented Sep 29, 2011 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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