4

For our 'Posts' type, our Contributors can see only their own posts, which is what we want. But for our custom post types, Contributors can see every post, including drafts. Is there a way to limit their view of custom posts so they only see their own posts like it is in the 'Posts' type?

Edit: Maybe I wasn't as clear about my problem. I want the Contributors to be able to see all of the custom types (Videos, Images, etc), but within each custom type I want them to be able to see only their own posts. So if they look at the CPT Videos, for example, they would only be able to see their own posted videos, not a post by someone else.

  • There are solutions about that here, have you searched? It's always nice to reserch before asking, and include any relevant info in the Question. Check this guide: How to Ask. – brasofilo Mar 4 '13 at 1:38
  • I did search before I asked, but I didn't see anything that dealt with my problem. Most were about blocking custom posts from being shown altogether to lower roles, which is not what I want to do. – user28330 Mar 4 '13 at 1:51
7

You need to use the action hook pre_get_posts.

See the comments for details and modify the Custom Post Type(s) to your own:

add_action( 'pre_get_posts', 'filter_cpt_listing_by_author_wpse_89233' );

function filter_cpt_listing_by_author_wpse_89233( $wp_query_obj ) 
{
    // Front end, do nothing
    if( !is_admin() )
        return;

    global $current_user, $pagenow;
    get_currentuserinfo();

    // http://php.net/manual/en/function.is-a.php
    if( !is_a( $current_user, 'WP_User') )
        return;

    // Not the correct screen, bail out
    if( 'edit.php' != $pagenow )
        return;

    // Not the correct post type, bail out
    if( 'portfolio' != $wp_query_obj->query['post_type'] )
        return;

    // If the user is not administrator, filter the post listing
    if( !current_user_can( 'delete_plugins' ) )
        $wp_query_obj->set('author', $current_user->ID );
}

You'll notice that the post count All|Published|Drafts needs to be corrected.
See the solution here.

  • 3
    With the current Wordpress version > 4.5 you should use: wp_get_current_user() instead get_currentuserinfo() (which has been deprecated) - no other changes are needed. – Picard May 5 '17 at 9:38

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.