WordPress.org

Support

Support » How-To and Troubleshooting » Exclude Posts in Dashboard based on User

Exclude Posts in Dashboard based on User

  • Hi
    I would like to hide 4 posts from the Post Edit Screen in the Dashboard (not the homepage of the website or any other page) from other logged in Users, apart from myself.

    I don’t want to password protect it or make it Private as the 4 posts are used on a Custom Dashboard page. If I can hide them then my client won’t be able to edit them.

    How do I do it, been searching all day and can’t find an answer.

    Colin

Viewing 3 replies - 1 through 3 (of 3 total)
  • Rod Whiteley
    Member

    @rod-whiteley

    My first thought is to filter posts_where when WP_ADMIN is defined, so as to exclude those post’s IDs, but I haven’t tried it.

    Hi Rod
    I have tried the code but with no luck.

    I’m thinking of something like this to added into functions.php:

    /** Hide SuperAdmin Posts **/
    function hide_posts-unless_superadmin() {
        $userdata = wp_get_current_user();
        if ( $userdata->ID != 1 ) {
            some code to hide superadmin posts...
        }
    }
    
    add_action('admin_init', 'hide_posts-unless_superadmin');

    But I’m stuck and maybe barking up the wrong tree.

    Colin

    Hi
    I found this piece of code on another thread:

    function posts_for_current_author($query) {
    	global $pagenow;
    
    	if( 'edit.php' != $pagenow || !$query->is_admin )
    	    return $query;
    
    	if( !current_user_can( 'manage_options' ) ) {
    		global $user_ID;
    		$query->set('author', $user_ID );
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'posts_for_current_author');

    This works great but I want to modify it so only I can access the posts and not other Admins.

    I have modified it and me being the SuperAdmin I now can’t see other posts by other users.

    function posts_for_current_author($query) {
    	global $pagenow;
        if ( $user_ID->ID != 1 ) {
    	    if( 'edit.php' != $pagenow && $user_ID->ID != 1 )
    	        return $query;
    
    	    if( $user_ID->ID != 1 ) {
    		    global $user_ID;
    		    $query->set('author', $user_ID );
    	    }
        }
    	return $query;
    }
    add_filter('pre_get_posts', 'posts_for_current_author');

    Any help please.

    Colin

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude Posts in Dashboard based on User’ is closed to new replies.