0

I'm trying to create a list on the frontend of my site of all media items that the current user has uploaded.

It seems like the way to do this would be to loop through and compare the ID of the current author with the ID of the author of the media item and then only display the items when a match is found.

But, I can't figure out how to get the author of the media item. Is there a function for this? Does someone have a better (easier) way to do this?

2

Media are just posts - so just query posts. This is untested:

$args = array(
    'author' => $author_id,
    'post_type' => 'attachment',
);
$query = new WP_Query( $args );

I'm not sure if you want currently logged in user or the author of the post currently in the loop - either way you can set $author_id above appropriately.

  • Ooooh, duh. That makes sense. Ended up with this, which works beautifully: if (is_user_logged_in()) { $user_id = get_current_user_id(); $the_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inheret', 'author' => $user_id) ); if ( $the_query->have_posts() ) while ( $the_query->have_posts() ) : $the_query->the_post(); etc etc etc – Eckstein Mar 26 '13 at 19:07
  • Cool. Note it should be 'inherit', though. – vancoder Mar 26 '13 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.