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

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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 );
    
  2. 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.