• Hi,
    I am wondering if I am able to get multiple input into ‘category__and’ (or ‘category’) argument separated by OR (or AND in ‘category’ case).

    I have something like this:

    $args = array(
    	      'posts_per_page'  => '10',
    	      'numberposts'     => '',
    	      'offset'          => 0,
                  'category__and'   => $category_array,
    	      'orderby'         => 'post_date',
    	      'order'           => 'DESC',
    	      'post_type'       => 'post',
    	      'post_status'     => 'publish',
    	      'suppress_filters' => false );   
    
          $myposts2 = new WP_Query( $args );

    And it works great. But yesterady, the task changed and user now can choose more options in one filter category. So I need change query to something like this:

    $args = array(
    	      'posts_per_page'  => '10',
    	      'numberposts'     => '',
    	      'offset'          => 0,
                  'category__and'   => <strong>$category_array_1 OR $category_array_2 OR $category_array_3</strong>,
    	      'orderby'         => 'post_date',
    	      'order'           => 'DESC',
    	      'post_type'       => 'post',
    	      'post_status'     => 'publish',
    	      'suppress_filters' => false );   
    
          $myposts2 = new WP_Query( $args );

    I can make those combination earlier in the code, but, I cant get this OR thing working and I can’t find any solution neither (maybe I am just blind). I am usualy capable to do my homewrok properly, but searchin for “OR” is kind of tricky…thanks for any help ??

    EDIT: I can probably create a workaround with multiple queries, but I’d like to have it in one query.

Viewing 1 replies (of 1 total)
  • I think you can get what you want using a tax_query. Try replacing your ‘category__and’ entry with this:

    'tax_query' => array(
       'relation' => 'OR',
       array(
          'taxonomy' => 'category',
          'field' => 'id',
          'terms' => $category_array_1
       ),
       array(
          'taxonomy' => 'category',
          'field' => 'id',
          'terms' => $category_array_2
       ),
       array(
          'taxonomy' => 'category',
          'field' => 'id',
          'terms' => $category_array_3
       )
    ),
Viewing 1 replies (of 1 total)
  • The topic ‘'category__and' with multiple input’ is closed to new replies.