1

I have two WP_Query like these:

Searching by s:

$query1 = new WP_Query([
  'post_type' => 'product',
  's' => $searchStr
]);

and searching by metabox:

$query2 = new WP_Query([
  'post_type' => 'product',
  'meta_query' => [
    [
      'key' => 'my_meta_key',
      'value' => $searchStr,
      'compare' => 'LIKE'
    ]
  ]
]);

How can I create a query to relate these two query by "OR", like:

`$query1 OR $query2" ?

1

Hope this code may work for you.

$query1 = new WP_Query( array(
        'post_type' => 'product',
        's' => $searchStr
    ));

    $query2 = new WP_Query( array(
        'post_type' => 'product',
        'meta_query' => array(
            array(
               'key' => 'my_meta_key',
               'value' => $searchStr,
               'compare' => 'LIKE'
            )
         )
    ));

    $result = new WP_Query();
    $result->posts = array_unique( array_merge( $query1->posts, $query2->posts ), SORT_REGULAR );
    $result->post_count = count( $result->posts );

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.