Created
December 15, 2020 19:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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