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.

[php]<?php
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;
}
[/php]

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Leave a Reply