Two different conditions with a single WP_Query

I need to get custom posts with next conditions:

  1. Custom post with meta 'key1' => 'public'
  2. Custom post with meta 'key1' => 'private' and 'author' => 'current_user_id'

Code:

Read More
$args = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
        'meta_key' => 'key1',
        'meta_value' => 'public'
);

$args2 = array(
        'post_type' => $post_type,
        'posts_per_page' => -1,
        'meta_key' => 'key1',
        'meta_value' => 'private',
        'author' => get_current_user_id()
);

$get_posts = new WP_Query($args);
$custom_posts = $get_posts->get_posts();

$get_posts_2 = new WP_Query($args2);
$custom_posts2 = $get_posts_2->get_posts();

and then I merge them:

$merged_posts = array_merge($custom_posts2, $custom_posts2);

It works, but I have problem with pagination and total items count. And it is two query instead of one.

How can I achieve this with a single query?

Related posts

Leave a Reply

1 comment

  1. So from my understanding:

    IF > User not Logged In – Show all ‘Public’ (meta) posts.

    ELSE IF > User Logged In – Show all ‘Public’ (meta) posts (from all users) AND all ‘Private’ (meta) posts from the current logged in user.

    I’ve run two queries below and combined the posts. It’s not without flaws, for instance: On the first page it shows more than posts_per_page parameter. I also couldn’t manually override the default posts_per_page without breaking pagination.

    You may need to save out permalinks to get this to work – hopefully it does!

    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
        $mainQuery = new WP_Query(array('meta_query' => array(array('key' => 'key1', 'value' => 'public'))));
    
        $postCount = $mainQuery->post_count;
    
        if(is_user_logged_in()){
            $secondQuery = new WP_Query(array('post', 'meta_query' => array(array('key' => 'key1', 'value' => 'private')), 'author' => get_current_user_id(), 'paged' => $paged));
            $postCount = ($postCount + $secondQuery->post_count);
    
            $mainQuery->posts = array_merge($mainQuery->posts, $secondQuery->posts);
            $mainQuery->post_count = $postCount;
            $mainQuery->max_num_pages = ceil($postCount / $posts_per_page);
        }
    ?>
    
    <?php if($mainQuery->have_posts()) : ?>
        <?php while($mainQuery->have_posts()) : $mainQuery->the_post(); ?>
            <h1><?php the_title(); ?></h1> 
        <?php endwhile; ?>
    <?php endif; ?>
    
    <?php if($mainQuery->max_num_pages > 1) : ?>
        <?php 
            $big = 9999999999;
            $args = array(
                'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'        => '?paged=%#%',
                'current'       => max( 1, $paged ),
                'total'         => $mainQuery->max_num_pages,
                'end_size'      => 0,
                'mid_size'      => 2,
                'prev_next'     => False
            ); 
            echo paginate_links($args); 
        ?>
    <?php endif; ?>