[英]Wordpress - Make it so that non-admin users can't see posts with a specific custom post status
我将对功能文件使用什么样的钩子才能使其生成,以便所有非管理员用户都无法在wp-admin后端看到具有特定自定义post_status的所有帖子。 但是仍然可以通过WordPress发布循环查询和循环吗?
使用pre_get_posts,您应该可以入门(从管理屏幕隐藏帖子)。 您可能还需要检查帖子类型等。
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 );
要禁止用户编辑具有特定状态的帖子,您应该执行以下操作:
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.