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 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:

$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?

share|improve this question

1 Answer 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; ?>
share|improve this answer
    
I need to get all posts that have 'key1' => 'public' and any author and posts with 'key1' => 'private' AND 'author' => get_current_user_id(). In your query we will have only your own created posts with public and private meta key. –  Ilya Mar 15 at 18:28
1  
I've updated my answer, try that out, hopefully it will point you in the right direction at least. –  Howdy_McGee Mar 15 at 19:18
    
Thanks for the answer, but if user logged it will take only posts where he is author. I need all public posts from any author. –  Ilya Mar 15 at 21:49
    
Ok, so lemme get this straight here. IF user is Guest (not logged in) it shows all Public (meta) posts. ELSE user is logged in, show all Public (meta) posts AND Private (meta) posts from only that user. So no matter what we are going to show all Public (meta) posts, does this sound correct? –  Howdy_McGee Mar 15 at 21:52
    
Also, is public and private actual meta_keys or are they set via post_status? –  Howdy_McGee Mar 15 at 22:00

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.