简体   繁体   中英

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. BUT it is still able to be queried and looped through the WordPress post loop?

With pre_get_posts you should be able to get started (to hide posts from the admin screen). 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM