[英]Wordpress - Make it so that non-admin users can't see posts with a specific custom post status
What hooks would I use for the functions file in order to make it so that all non-admin users can't see all posts with a specific custom post_status in the wp-admin back-end. 我将对功能文件使用什么样的钩子才能使其生成,以便所有非管理员用户都无法在wp-admin后端看到具有特定自定义post_status的所有帖子。 BUT it is still able to be queried and looped through the WordPress post loop?
但是仍然可以通过WordPress发布循环查询和循环吗?
With pre_get_posts you should be able to get started (to hide posts from the admin screen). 使用pre_get_posts,您应该可以入门(从管理屏幕隐藏帖子)。 You may also want to check the post type, etc.
您可能还需要检查帖子类型等。
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
To disallow users to edit posts with that specific status, you should do: 要禁止用户编辑具有特定状态的帖子,您应该执行以下操作:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.