Show only posts and media owned by logged in WordPress user

How to disable showing of posts and media of other WordPress users

If you’ve ever worked on a WordPress community site supported by a lot of publishers, you might have noticed that even though you can’t edit others post, you can still view the titles of every post inside the WordPress admin. It is because by default, WordPress allows authors to see the titles of every published post, drafts, and media.

In this tutorial, we’ll show you how to disable showing posts and media of other users.

Please remember to make a Wordpress backup before you proceed in case something breaks whilst tweaking the code.

1. Disable showing posts and media of other Wordpress users

Add the below code to your functions.php file to disable showing posts and media of other users.

add_action('pre_get_posts', 'query_set_only_author' );

function query_set_only_author( $wp_query ) {

global $current_user;

if( is_admin() && !current_user_can('edit_others_posts') ) {

$wp_query->set( 'author', $current_user->ID );

add_filter('views_edit-post', 'fix_post_counts');

add_filter('views_upload', 'fix_media_counts');

}

}

2. Disable showing of Wordpress post counts

As mentioned, the above given code will only disable showing titles of others’ posts inside the WordPress dashboard. The problem is that it won’t disable showing the post counts inside the ‘all posts’, ‘drafts’, ‘trash’ and ‘pending’ filter bars. If you would like to fix the post count in the filter bars as well, add the below code snippet to your theme’s functions.php file.

Please note that the below code will work only if it is used along with the above given code.

3. Fix media counts

If you want to disable the media counts as well, add the below code to your functions.php file. In order to make this code works, make sure you add the first code as well, that is, the code that disable showing post and media of other users.

One more thing... Did you know that people who share useful stuff like this post look AWESOME too? ;-) Please leave a useful comment with your thoughts, then share this on your Facebook group(s) who would find this useful and let's reap the benefits together. Thank you for sharing and being nice!

The Outstanding HungryJPEG Bundles

Advertise on CollectiveRay.com

CollectiveRay (formerly known as DART Creations) is interested in developing partnerships with mutual benefit. If you like the stuff we publish and would like to develop a relationship, we'd be happy to hear from you. Go on - drop us a line - we'd love to hear from you :-)

Disclosure: CollectiveRay is funded personally out of pure passion for helping people working with websites. We do however generate some income through recommendations of products. This means if you click on a link and purchase an item we link to, we will receive a small sum out of that sale. We usually partner with vendors to make your purchase cheaper than buying direct.

who are we?

CollectiveRay is run by David Attard - working in and around the web design niche for more than 12 years, we provide actionable tips for people who work with and on websites. We also run DronesBuy.net - a website for drone hobbyists.