Question

How do you show media posted by a certain user in Wordpress? Say I have the loop... and im using get_the_author_meta() to retrieve user inf Now I want to show all the images a certain user has uploaded. Have no idea where to begin. Just want to show a list of images that one user has uploaded.

Was it helpful?

Solution

A very common code to allow users to see ONLY their own attachments in the upload page is :

add_action('pre_get_posts','users_attachments');
function users_attachments( $wp_query_obj ) {

    global $current_user, $pagenow;

    if( !is_a( $current_user, 'WP_User') )
        return;

    if( 'upload.php' != $pagenow )
        return;

    if( !current_user_can('delete_pages') )
        $wp_query_obj->set('author', $current_user->id );

    return;
}

So based on this, and removing the conditionals you do not need - you could try :

   add_action('pre_get_posts','users_attachments');

    function users_attachments( $wp_query_obj ) {

            $wp_query_obj->set('author', '30' ); // ID of user , or any other meta..

        return;
    }

or Filter the Query for example :

function user_files_only( $wp_query ) {


            global $current_user;
            $wp_query->set( 'author', '30' ); // ID of user , or any other meta..

}

add_filter('parse_query', 'user_files_only' );

Both examples actually alter the main query in some way , so you might want to remove the action after applying it . Not sure how this will work on the loop.

OTHER TIPS

You could use the pre_get_posts Filter and to filter the query..

Give a tag as user x for a certain user who posts his media in different categories, now it is easy if anyone press that tag then all the media uploaded by a particular user gets displayed one by one.(i mean that you must use tags for all the posts of the user-x by giving the same tag for all his uploaded media )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top