Implementing an OR statement to wordpress wp_query

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

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 );

Leave a Comment