Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created December 15, 2020 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/88ffd2d6e49bf0a96c50236d5096edee to your computer and use it in GitHub Desktop.
Save nfsarmento/88ffd2d6e49bf0a96c50236d5096edee to your computer and use it in GitHub Desktop.
WordPress Allows contributors to see and manage only their custom post types and drafts from the manage posts screen.
/*
*
* Allows contributors to see and manage only their custom post types and drafts from the manage posts screen.
* src: https://wordpress.stackexchange.com/questions/89233/restrict-contributors-to-view-only-their-own-custom-post-types
*
*/
add_action( 'pre_get_posts', 'aet_filter_cpt_listing_by_author' );
function aet_filter_cpt_listing_by_author( $wp_query_obj ){
// Front end, do nothing
if( !is_admin() )
return;
global $current_user, $pagenow;
wp_get_current_user();
// 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( 'tribe_events' != $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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment