Only Show an Author Their Custom Post Types

I have a site with 3 different custom post types (Listings, Contacts, Events) and about 1600 ‘authors’.
What I”m looking to do is only show that author their posts in the backend. So when authorA logs in, and clicks on ‘Listings’ then they only see their own.

I have this code running in Functions.php to control the first post type, but am not sure how to control the other two. The issue i’m seeing is that when authorA clicks on ‘Events’, their post that is in ‘Listings’ show up.

    function __set_all_posts_for_author( &$query )
{
    if ( $query->is_author )
        $query->set( 'post_type', 'listings' );
    remove_action( 'pre_get_posts', '__set_all_posts_for_author' ); // run once!
}
add_action( 'pre_get_posts', '__set_all_posts_for_author' );

So, i think i just need to change ‘listings’ to all three post types show up but i’m not sure how….
Any thoughts?

2 Answers
2

As @s_ha_dum says in his comment, your description and your code seems to be different.

If I understand the description, the code to do what you want is:

function __set_all_posts_for_author( $query ) {
  if ( is_admin() && is_post_type_archive( array('listings', 'contacts', 'events') ) ) {
    $current_user = wp_get_current_user();
    $query->set( 'author', $current_user->ID );
  }
}
add_action( 'pre_get_posts', '__set_all_posts_for_author' );

Using the code above, every time an user that is logged in the backend require items list of CPT ‘Listings’, ‘Contacts’ and ‘Events’ he/she will see only the post created by himself/herself.

Leave a Comment