Limit Admin Page Editing Capabilities to Page Owners by Capability

Currently, WordPress does a great job displaying pages on the Edit Pages admin area for users with the capability edit_others_pages. However, if a user has edit_others_pages and has not yet authored any pages, WordPress defaults to listing all the pages removing edit actions, etc. Recently I had the need to list NO PAGES/POSTS except what the person has authored.

add_action( 'pre_get_posts', 'wps_admin_exclude_pages' );
 * Modifies the admin query for a specific author for the Edit Posts
 * and Pages admin pages checking capability.
 * @param WP_Query Object $query Original Query Object
 * @return WP_Query Object $query Original/Modified Query Object
function wps_admin_exclude_pages( $query ) {
    if( ! is_admin() )
        return $query;
    global $pagenow, $user_ID;
    if( 'edit.php' == $pagenow && 'page' == get_query_var( 'post_type' ) && ! current_user_can( 'edit_others_pages' ) )
        $query->set( 'author', $user_ID );
    if( 'edit.php' == $pagenow && 'post' == get_query_var( 'post_type' ) && ! current_user_can( 'edit_others_posts' ) )
        $query->set( 'author', $user_ID );

    return $query;
About Travis Smith

As a WordPress Enthusiast, Travis writes about his journey in WordPress trying to help other WordPress travelers and enthusiasts with tutorials, explanations, & demonstrations of the things he learns.

Leave a Reply

%d bloggers like this: