0

I'd like to allow users who are logged in and have the author role to be able to view drafts from other users, but not to edit them (the editors can do that).

I've managed to allow these author users to see others' drafts on the archive page where they would display like this (I have a different CSS treatment for unpublished items):

if ( ! function_exists( 'see_unpublished_posts' ) ) :
    add_action( 'pre_get_posts', 'see_unpublished_posts' );
    function see_unpublished_posts( $query ) {
        if ( ! is_admin() && current_user_can( 'view_unpublished_posts' ) ) {
            if ( $query->is_main_query() ) {
                $query->set( 'post_status', 'any' );
            }
        }
        return $query;

    }
endif;

This works if I assign the view_unpublished_posts capability to those users (I'm using the AAM plugin to assign capabilities).

The problem is that when the author user clicks on an unpublished draft, they see the post as a 404 (the /?p=186042 URL).

One additional thing I tried was adding this code to the same method:

if ( is_singular( 'post' ) ) {
    $query->set( 'post_status', array( 'publish', 'draft', 'future' ) );
}

I tried this both with $query->is_main_query() and without. It almost works, but it also causes all kinds of problems because it loads any draft post. For example sometimes we have bottom modal windows that are their own post type, and these types of posts all render in the markup for these users.

What can I do to have these author users able to view - only the post they're trying to load - even if it is a draft?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.