I want to display all post under a category and all subcategories under the parent categories. But couldn't find proper suggestion over forum or failed to understand the solution. It's possible to perform this tasks by manually adding add ID into query but for me, I need all to be autopopulated.

Need suggestion.


The cat and category_name parameters for WP_Query display "posts that have this category (and any children of that category)".

There are also examples of doing this in the Codex.

$query = new WP_Query( 'cat=4' );
// or
$query = new WP_Query( 'category_name=staff' );

I am not 100% sure what you mean by "I need all to be autopopulated", but if you want WordPress to "just know" what category you want there is no such ESP-based function in either WordPress or PHP. You have to tell the query what category to use, but you don't have to hard-code to ID or the name. It may be possible to logically derive the appropriate category but you have not provided any details at all about what the criteria would be for making that decision. You don't even say what page you are on, or whether you talking about the front or the back end. Without that detail a better answer is going to be difficult.

| improve this answer | |
  • For me, new WP_Query( 'cat=4' ); returned only posts from parent category, not the subcategory under category ID 4. – IFightCode May 22 '13 at 14:00
  • 1
    I get posts in child categories. Analyze what is happening carefully. If you really are not getting posts assigned to child categories something is interfering with the query, perhaps a filter. – s_ha_dum May 22 '13 at 14:09

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.