1

I need to get posts belongs to few categories, but it should match following rule.

let say I have category ids 100,105 & 106.

then I need 100 && ( 105 || 106 ) this rule.

I know following rules for separate OR & AND,

$query = new WP_Query( array( 'cat' => '100,105,106' ) );  // 100 || 105 || 106
$query = new WP_Query( array( 'category__and' => array( 100,105,106 ) ) ); // 100 && 105 && 106

But I need something like 100 && ( 105 || 106 ). How can I do that with WP_Query ?

1

Not sure this is the best way, but I have managed to get the desired result set with following $args parameters.

$args['tax_query'] =    array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field'    => 'id',
        'terms'    => array(100),
    ),
    array(
        'taxonomy' => 'category',
        'field'    => 'id',
        'terms'    => array(105,106),
    ),
);
-1

Set up your terms in php beforehand and then pass them to wp_query as a variable.

$mycats = array( ...
   some logic
);

and then

$query = new WP_Query( array( 'cat' => $mycats ) );
  • ...perhaps you'd like to explain the down votes? – Chris Pink Jul 26 '17 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.