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.

 

 

Wordpress shows only wordpress posts and media owned by logged in user

titles view disabled

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.

function fix_post_counts($views) {

 global $current_user, $wp_query;

 unset($views['mine']);

 $types = array(

    array( 'status' =>  NULL ),

    array( 'status' => 'publish' ),

    array( 'status' => 'draft' ),

    array( 'status' => 'pending' ),

    array( 'status' => 'trash' )

 );

 foreach( $types as $type ) {

    $query = array(

        'author'   => $current_user->ID,

        'post_type'   => 'post',

        'post_status' => $type['status']

    );

    $result = new WP_Query($query);

    if( $type['status'] == NULL ):

        $class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : '';

        $views['all'] = sprintf(__('<a href="/%s"'. $class .'>All <span class="count">(%d)</span></a>', 'all'),

            admin_url('edit.php?post_type=post'),

            $result->found_posts);

    elseif( $type['status'] == 'publish' ):

        $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';

        $views['publish'] = sprintf(__('<a href="/%s"'. $class .'>Published <span class="count">(%d)</span></a>', 'publish'),

               admin_url('edit.php?post_status=publish&post_type=post'),

            $result->found_posts);

    elseif( $type['status'] == 'draft' ):

        $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';

        $views['draft'] = sprintf(__('<a href="/%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),

            admin_url('edit.php?post_status=draft&post_type=post'),

            $result->found_posts);

    elseif( $type['status'] == 'pending' ):

        $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';

        $views['pending'] = sprintf(__('<a href="/%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'),

               admin_url('edit.php?post_status=pending&post_type=post'),

            $result->found_posts);

    elseif( $type['status'] == 'trash' ):

        $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';

        $views['trash'] = sprintf(__('<a href="/%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'),

            admin_url('edit.php?post_status=trash&post_type=post'),

            $result->found_posts);

    endif;

 }

 return $views;

}

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.

function fix_media_counts($views) {

 global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types;

 $views = array();

 $_num_posts = array();

 $count = $wpdb->get_results( "

    SELECT post_mime_type, COUNT( * ) AS num_posts

    FROM $wpdb->posts

    WHERE post_type = 'attachment'

    AND post_author = $current_user->ID

    AND post_status != 'trash'

    GROUP BY post_mime_type

 ", ARRAY_A );

 foreach( $count as $row )

    $_num_posts[$row['post_mime_type']] = $row['num_posts'];

 $_total_posts = array_sum($_num_posts);

 $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );

 if ( !isset( $total_orphans ) )

    $total_orphans = $wpdb->get_var("

        SELECT COUNT( * )

        FROM $wpdb->posts

        WHERE post_type = 'attachment'

        AND post_author = $current_user->ID

        AND post_status != 'trash'

        AND post_parent < 1

    ");

 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));

 foreach ( $matches as $type => $reals )

    foreach ( $reals as $real )

        $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];

 $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : '';

 $views['all'] = "<a href='upload.php'$class>" . sprintf( __('All <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>';

 foreach ( $post_mime_types as $mime_type => $label ) {

    $class = '';

    if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )

        continue;

    if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )

        $class = ' class="current"';

    if ( !empty( $num_posts[$mime_type] ) )

        $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>';

 }

 $views['detached'] = '<a href="/upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Unattached <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>';

 return $views;

 And that ladies and gentleman is a code hack which allows you to only show the posts and media of the currently logged in WordPress user.

Download the list of 101 WordPress tricks every blogger should know

101 WordPress tricks

Click here to Download Now

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!

Featured On

Inc Magazine Logo  

Sitepoint logo  

CSS Tricks logo   

webdesignerdepot logo   WPMU DEV logo   

and many more!

Where are we hosted?

This site is proudly powered by FAST VPS InMotion Servers and given an insane speed thanks to MaxCDN!

Web Hosting MaxCDN - Speed up your website

New! DIVI 3.1 WordPress Template + PageBuilder

You'll surely create a perfect website with Divi 3.1!

Get an exclusive 10% OFF for CollectiveRay visitors until

Divi 3

 

WordPress Elementor Sale

Themes for the most popular WordPress page builder from TemplateMonster!

We've got an exclusive 10% OFF for CollectiveRay visitor until - use Coupon Code: collectiveray

TemplateMonster Elementor sale

 

TemplateMonster Bundles

Awesome bundles for awesome websites. $1300 worth of products for $49 only!

We've got an exclusive 10% OFF for CollectiveRay visitors until - use Coupon Code: collectiveray

TemplateMonster Bundles

How to make your website FAST!

Step-by-step - free email course, how to make your website load in less than 1 second 

The Outstanding HungryJPEG Bundles

AWESOMENESS! Bundles of premium font + graphic packs at more than 96% OFF!  Get a bundle for just $9 - ONLY!

The Hungry JPEG Awesome font 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.

David attard

Follow us on Social

Follow us on Facebook   Follow us on Google+   Subscribe to our RSS Feed   Follow us on Twitter