WordPress.org

Ready to get started?Download WordPress

Forums

Get posts by combination of multiple categories using WP_Query (4 posts)

  1. Asaf Cohen
    Member
    Posted 1 year ago #

    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!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

  3. Asaf Cohen
    Member
    Posted 1 year ago #

    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);
  4. esmi
    Forum Moderator
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic