• Resolved umchal

    (@umchal)


    Hi there,

    I’m wondering if there is a way to perform a single WP_Query query with the following conditions:

    – retrieve posts of the custom post type test
    – AND with the post status of publish posted by all users
    – AND the post status of private of the logged-in user (meaning not retrieving somebody else’s private posts)

    I first thought this can be achieved with two separated queries but when merging the results, the sort order will be messed. Also to keep the number of database queries at minimum is preferable.

    If you know a good way of doing this, it would be appreciated. Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Also, merged queries makes pagination difficult.

    Yes, that is possible. It’s actually quite simple because that is how WP_Query works by default. The exception is users with “read_private_posts” capability will be able to see other user’s private posts (editors and admins). Regular, less privileged users can only see their own private posts.

    Here’s the SQL WP_Query creates by default for a custom post type when author role user ID 4 is logged in:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE wp_posts.post_type = 'test' AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 4 AND wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    There are a number of filters available where you could alter this query directly, or do so by changing query vars in a callback to “pre_get_posts” action.

    Thread Starter umchal

    (@umchal)

    Hi,

    So do you mean you have to manually write a SQL query? It is what you mean by default? I might be missing something.

    When you do this,

    
    $q = new WP_Query(
        array(
            'post_type' => 'test'
        )
    );
    $q->request
    

    by default, the generated database query by WordPress is like this

            
    SELECT SQL_CALC_FOUND_ROWS  wptests_posts.ID FROM wptests_posts  WHERE 1=1  AND wptests_posts.post_type = 'test' AND (wptests_posts.post_status = 'publish')  ORDER BY wptests_posts.post_date DESC LIMIT 0, 10
    

    It does not handle the asked conditions like the private post status and post_author.

    Thread Starter umchal

    (@umchal)

    Ah,

    When I test it with a logged-in user, actually the generated query is like this

    
    SELECT SQL_CALC_FOUND_ROWS  wptests_posts.ID FROM wptests_posts  WHERE 1=1  
    AND wptests_posts.post_type = 'test' 
    AND (wptests_posts.post_status = 'publish' OR wptests_posts.post_status = 'private')  
    ORDER BY wptests_posts.post_date DESC LIMIT 0, 10
    

    The part AND (wptests_posts.post_status = 'publish' OR wptests_posts.post_status = 'private') is indeed automatically added by default. Maybe this is what you mean.

    Still, the post author’s part is missing though.

    Thread Starter umchal

    (@umchal)

    Wait, I was testing with the administrator privilege. When I test with the subscriber user roll, the query indeed is what you said.

    So, it’s all done by default.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. I was pleased to see you figure this out for yourself through your series of replies. The best way to learn things IMO ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WP_Query: retrieve posts of publish and private of the logged-in user’ is closed to new replies.