WordPress.org

Ready to get started?Download WordPress

Forums

query posts using array, not pulling in child category posts (2 posts)

  1. mparascandola
    Member
    Posted 2 years ago #

    Hello, Im using <?php query_posts(array('category__and'=>array(9763, 9751))); ?> where 9763 is category "A" and 9751 is category "B" which has many child categories.

    I would like to pull in all posts that are part of cat A and cat B (or all A and child of B)

    Issue is that any posts that are not directly in B but are only a child of B do not appear.

  2. stvwlf
    Member
    Posted 2 years ago #

    Codes explains that category__and will not pull in child categories

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Multiple Category Handling

    Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

    $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

    To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

    One approach is select all 9763 posts using get_posts. This puts them in an array. Then loop through the array, selecting posts that are in 9751 or its child categories. (First make an array of all the child categories of 9751) It requires some custom code. I googled looking for an article for you to copy but didn't find one.

Topic Closed

This topic has been closed to new replies.

About this Topic