StackTips
 2 minutes

How to Limit Authors and Contributors to their Own Post in WordPress

By Editorial @stacktips, On Sep 17, 2023 Wordpress 2.58K Views

If you running a multi-author site, you will notice that all authors and contributors are able to see the posts from other users. Although they cannot edit or modify, you may not want them to see the posts which are under review or in the draft.

You can do this by installing third-party WordPress plugin such as View Own Post Media Only. But for this little job, I personally do not recommend to use any plugins.

This little code hack will help you to limit your authors and contributors to their own posts in WordPress admin. Copy and past the following code in you function.php file.

if (current_user_can('contributor') || current_user_can('author')){
	add_filter('parse_query', 'filter_my_own_posts_query' );
}

function filter_my_own_posts_query( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
      global $current_user;
      $wp_query->set( 'author', $current_user->id );
     }
}
stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.