Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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',
                )
            )
        );
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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 );
share|improve this answer
    
Adapted from @Chip Bennett's answer on a different question –  bungeshea Jan 9 '13 at 12:40
    
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() –  Rohit Pande Jan 9 '13 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 '13 at 12:51
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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