0

As an admin, I can preview scheduled posts. In the post listing, there's a "preview" link which appears if you hover over the scheduled post title.

As a contributor, I can preview a post if it is in draft or pending. I can no longer preview the post once it has been scheduled.

Contributors are curious to see what the editor has made of their post and would like to see a preview of it while it is scheduled and not yet published. To be clear: I'm not looking for a way to allow the contributor to alter the post once it is scheduled, just to (pre)view it.

In the wordpress roles and capabilities, I'm not finding a related capability. There seems to be read, read_private_posts, read_private_pages, but nothing like read_own_future_posts. I'm not sure why this is not even the default behaviour. It's odd the contributor can view an own post to write it and once it's published, but not in between. Or am I missing something completely here?

0

You can write your query for listing scheduled posts as follow : You can see the details here

$the_query = new WP_Query(array( 
    'post_status' => 'future',
    'posts_per_page' => 3,
    'orderby' => 'date',
    'order' => 'ASC'
));

To show the scheduled post on details page you should do something like this Add code to your theme's functions.php file. Details can be seen here

add_filter('the_posts', 'show_future_posts');
function show_future_posts($posts) {
   global $wp_query, $wpdb;
    if(is_single() && $wp_query->post_count == 0) {
        $posts = $wpdb->get_results($wp_query->request);
    }
   return $posts;
}
  • So your solution is to... Create a custom page? I guess that could be an option. It would have to include also checking the current user to only show the future posts of that user. And even then, it won't really show the post like it will be in the site. This might work, but it's not really what I'm looking for. I would like contributors to have a "preview" link on their scheduled posts in the regular posts page they see once logged in. – Horakiri Suo Mar 27 '18 at 18:48
0

I've created a custom plugin for this. If you don't know how to work with that, read the basics on the wordpress site. It's not difficult even if you're not technical: basically go to wp-content/plugins, create a folder with the plugin name, create a .php file with the plugin name, add a header and paste the below code blocks.

Then upload the file, go to your wordpress site. You'll see your plugin appearing in the installed plugins. Activate it. Done.


Step 1: if a post author has a preview link, they should be able to preview the post. This works for contributors but should really work for any role.

//allow post preview if you are the post owner, whatever role you might have (e.g. contributor)
function jv_change_post( $posts ) {
    if(is_preview() && !empty($posts)){
        $current_user_id = get_current_user_id();
        $author_id= $posts[0]->post_author;
        if($current_user_id == $author_id)
            $posts[0]->post_status = 'publish';
    }

    return $posts;
}
add_filter( 'posts_results', 'jv_change_post', 10, 2 );

As far as I understand it, what this does is: whenever wordpress does a query for posts, check if the current page being shown is a post preview. If it is and the current user is the author of the post, change the status to publish. This will allow the author to see the post, no matter what the real status of the post is (e.g. "future" if it's planned).

This doesn't change the status of the post for real, it just makes the current query result think that the post is published (and thus can be viewed).

Note: I got the core of this code from another stackexchange post.


Step 2: how to get the post author a preview link in the first place? I solved this by adding the link as a column in the posts-screen (admin section). This is the code:

//add preview link to posts overview in admin section (as extra column)
// add the column
function add_contributor_preview_column( $columns ) {
    if( !current_user_can( 'edit_others_posts' ) ) {
        $previewColumn = array( 'preview' => __( 'Preview', 'your_text_domain' ) );
        return array_merge($previewColumn, $columns);
    }
    else
        return $columns;
}
add_filter( 'manage_posts_columns' , 'add_contributor_preview_column' );

// add contents to the column
function display_post_preview( $column, $post_id ) {
    if($column == "preview"){
        $post = get_post($post_id);
        if($post->post_status == 'future')
            echo '<a href="/?p=' . $post_id . '&preview=true">preview</a>';
        else
            echo '<a href="'. get_permalink($post_id) .'">view</a>';
    }
}
add_action( 'manage_posts_custom_column' , 'display_post_preview', 10, 2 );

While I was at it anyway, I filtered out posts in that admin screen. Contributors only see their own posts and don't get confused. This part is not directly related to the question, but it helps clean up the interface for contributors.

//only show your own posts in the edit page if you're a contributor (avoid confusion)
function posts_for_current_author($query) {
    if(!is_admin())
        return $query;

    if( !current_user_can( 'edit_others_posts' ) ) {
        $current_user_id = get_current_user_id();
        $query->set('author', $current_user_id );
    }
    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.