WordPress.org

Support

Support » How-To and Troubleshooting » Get posts by combination of multiple categories using WP_Query

Get posts by combination of multiple categories using WP_Query

  • Hey there,
    I’m using WP_Query function to select some posts inside some categories.

    'tax_query' => array(
    			array(
    			  'terms' => '1,2,3,4,20'
    			)

    This code above, give me all posts that are in the category 1 OR 2 OR 3 OR 4 OR 20.

    I want to get all posts that are in the category 1 AND 2 AND 3 AND 4 AND 20….
    Someone please have any claw how to do it?

    thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    @esmi

    Forum Moderator

    Hey, thanks for the fast reply.

    I’m using “category__and” and I get nothing… is something worng with the code? (there is a product at both categories and I didn’t get any PHP errors.

    $query_args = array( 'category__and' => array( 43, 29 ),  'posts_per_page' => 9, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product');
    $r = new WP_Query($query_args);
    esmi

    @esmi

    Forum Moderator

    Try query_args = array( 'cat' => 43, 29, 'posts_per_page' => 9, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product');

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get posts by combination of multiple categories using WP_Query’ is closed to new replies.
Skip to toolbar