Allow to see only the media files and images uploaded by the user in wordpress

Hi in this tutorial we are going to learn how to make Allow to see only the media files and images uploaded by the user in wordpress.

Allow to see only the media files and images uploaded by the user in wordpress

Write the code in functions.php file inside your theme folder
<?php
add_action(‘pre_get_posts’,’hm_users_own_attachments’);
function hm_users_own_attachments( $wp_query_obj ) {

global $current_user, $pagenow;

$is_attachment_request = ($wp_query_obj->get(‘post_type’)==’attachment’);

if( !$is_attachment_request )
return;

if( !is_a( $current_user, ‘WP_User’) )
return;

if( !in_array( $pagenow, array( ‘upload.php’, ‘admin-ajax.php’ ) ) )
return;

if( !current_user_can(‘delete_pages’) )
$wp_query_obj->set(‘author’, $current_user->ID );

return;
}
?>

Please provide your valuable comment to make the site more useful.

Thanks for reading

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.