简体   繁体   English

WordPress-做到这一点,以使非管理员用户无法看到具有特定自定义帖子状态的帖子

[英]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.

相关问题 在WordPress中从非管理员用户发布数据 - Post data from non-admin user in WordPress 将Wordpress中自定义字段的内容隐藏到非管理员 - Hiding the Content of a custom field in Wordpress to non-admin 为什么Wordpress为/ wp-admin和/ wp-content / plugins为非管理员用户使用cookie - Why does Wordpress use cookies for /wp-admin and /wp-content/plugins for non-admin users 如何使用 JWT 和 flutter_wordpress 包在 Flutter 应用程序中对 WordPress 非管理员用户进行身份验证? - How do I authenticate WordPress non-admin users in a flutter app with JWT and flutter_wordpress package? WP Admin:筛选自定义分类法并查看任何/所有状态的帖子? - WP Admin: Filter for a Custom Taxonomy and see posts of any/all status? Wordpress 在 wordpress 管理中隐藏自定义帖子类型的子帖子 - Wordpress Hide child posts of custom post type in wordpress admin 允许来自特定链接的用户查看WordPress中的私人帖子 - Allow users coming from a specific link to see private posts in WordPress 如何为非管理员用户隐藏仪表板菜单项 - How can I hide a dashboard menu item for non-admin users WordPress-看不到个别帖子 - Wordpress - Can't See Individual Posts 获取Wordpress帖子,以便诸如get_post_thumbnail_id之类的东西不会进行数据库调用 - Get Wordpress posts so things like get_post_thumbnail_id don't make a database call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM