2

I am trying to return all posts that meet the following conditions:

The category must be 63

OR

the post must have a value associated with the meta key named 'android_link'

My current argument array currently does not work because it is asking for both of these conditions to be met. Is it possible to change it so that only one of these conditions must be met to return a post?

    $args = array(
        'paged' => $paged,
        'order' => 'DESC',
        'meta_key' => 'android_link',
        'post_type' => array( 'post', 'app' ),
        'cat' => '63',

        'meta_query' => array(
            array(
                'key' => 'android_link',
                'compare' => 'EXISTS',
                )
            )
        );

2 Answers 2

0

You can run two queries and merge the results:

$cat_query_args = array(
    'paged' => $paged,
    'order' => 'DESC',
    'meta_key' => 'android_link',
    'post_type' => array( 'post', 'app' ),
    'cat' => '63',
);

$meta_query_args = array(
    'meta_query' => array(
        array(
            'key' => 'android_link',
            'compare' => 'EXISTS',
        )
    )
);

$cat_query  = new WP_Query( $cat_query_args  );
$meta_query = new WP_Query( $meta_query_args );

$merged_query_args = array_merge( $cat_query, $meta_query );
$merged_query = new WP_Query( $merged_query_args );
3
  • Adapted from @Chip Bennett's answer on a different question
    – shea
    Jan 9, 2013 at 12:40
  • 1
    In Chip's answer he is referring to add two arrays of arguments. But you are trying to merge the output of queries and passing that as a parameter to another query. This might lead to errors as it can cause invalid parameters for WP_Query() Jan 9, 2013 at 12:49
  • Thanks for the reply, but I get the following error: array_merge(): Argument #1 is not an array - This is becuase WP_Query returns an object. How do I turn it into an array?
    – richelliot
    Jan 9, 2013 at 12:51
0

You can run two queries and iterate through the results separately.

$paged = (get_query_var('paged')) get_query_var('paged');

$cat_query_args = array(
    'paged' => $paged,
    'order' => 'DESC',
    'meta_key' => 'android_link',
    'post_type' => array( 'post', 'app' ),
    'cat' => '63',
);

$meta_query_args = array(
    'meta_query' => array(
        array(
            'key' => 'android_link',
            'compare' => 'EXISTS',
        )
    )
);

$cat_query  = new WP_Query( $cat_query_args  );
$meta_query = new WP_Query( $meta_query_args );

Run a simple while loop for $cat_query and after that run another while loop for the results of $meta_query. This will create the same effect as you are performing OR operation on your query parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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