这可以分两步完成。首先,在页面中添加一个挂钩/wp-admin/edit.php?post_type=page
删除其他用户显示的所需页面。和另一个钩子,用于重定向未经授权的用户,使其无法直接访问页面/wp-admin/post.php?post=ID&action=edit
.
此处,职位类型为page
, 但它可以更改为任何其他。进行注释中所示的调整:
/**
* Adjust the following:
* post_type
* User ID
* Post ID
*/
add_action( \'load-edit.php\', \'load_custom_filter_wpse_94387\' );
add_action( \'load-post.php\', \'block_page_access_wpse_94387\' );
/**
* Executed only in /wp-admin/edit.php
*
* Checks current post type and bail if not correct
*/
function load_custom_filter_wpse_94387()
{
global $typenow;
// Not the correct post type, do nothing
if( \'page\' != $typenow ) // <--- adjust
return;
add_filter( \'posts_where\' , \'posts_where_wpse_94387\' );
}
/**
* If not authorized user, remove page from listing
*/
function posts_where_wpse_94387( $where )
{
$current_user = wp_get_current_user();
if ( 2 == $current_user->ID ) // <--- adjust
return $where;
$where .= \' AND ID != 119\'; // <--- adjust
return $where;
}
/**
* Check if unauthorized user is trying to access restricted page
*/
function block_page_access_wpse_94387()
{
// Check for post=119, if it is not this one, do nothing
if( !isset( $_GET[\'post\'] ) || \'119\' != $_GET[\'post\'] ) // <--- adjust
return;
// Check for user, if allowed user, do nothing
$current_user = wp_get_current_user();
if ( 2 == $current_user->ID ) // <--- adjust
return;
// Invalid attempt to edit the page, redirect
wp_redirect( admin_url( \'edit.php?post_type=page\' ) );
exit();
}
相关问答;答:
-Where to put my code: plugin or functions.php?-Update post counts (published, draft, unattached) in admin interface