WordPress.org

Support

Support » How-To and Troubleshooting » Query posts from an array of categories and their children

Query posts from an array of categories and their children

  • Drew Jaynes

    @drewapicture

    Docs Czar

    First, I’ve already checked the WP_Query Codex. I’ve already resigned myself to probably having to do a wpdb query, but here’s my problem:

    1. If I query a single category ID, it returns all posts in that category and that category’s children.
    2. Like this:

      Top Level Category
      	Post 1
      	Post 2
      	->Sub Category
      		Post 1
      		Post 2
      	->Sub Category
      		Post 1
      		Post 2
      		-> Sub Sub Category
      			Post 1
    3. If I query an array of category IDs, it returns all posts in those specific categories, but NOT their children.
    4. Like so:

      Top Level Category 1
      	Post 1
      	Post 2
      Top Level Category 2
      	Post 1
      	Post 2
      Top Level Category 3
      	Post 1
      	Post 2

    I need a way to query the equivalent of the first example with several top-level categories and their children, but grouped separately in an array (e.g. array(group1, group2, group3))

    Like so:

    Top Level Category 1
    	Post 1
    	Post2
    	->Sub Category
    		Post 1
    		Post 2
    Top Level Category 2
    	->Sub Category
    		Post 1
    	->Sub Category
    		Post 1
    Top Level Category 3
    	Post 1
    	->Sub Category
    		Post 1

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • jmoseley

    @jmoseley

    What does your query look like? Are you using category__in? If so, I think you will get the results you are looking for by querying a comma separated cat list. I have not tested this though…

    $query = new WP_Query( 'cat=2,6,17,38' );

    Drew Jaynes

    @drewapicture

    Docs Czar

    Thanks for the reply. I’m working on a pretty complicated multilayered query. I’m pulling the latest 3 posts from 6 top-level categories at once. I just recently realized it was only pulling posts actually IN the top-level cats and NOT their children.

    I’m going to try a couple of things. At this point, it could be the use of category__in on the array in the foreach, it (probably is) the array in get_categories. I’ll post back.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query posts from an array of categories and their children’ is closed to new replies.