List Files In WordPress’ Media Library

March 10, 2010 By: Leo Templin

Need to list some videos or music files on your blog or website but can’y figure out how to access all those files? No problem, I got your back, just read on.

Know your MIME types

Depending on the type of media you want to retrieve you need to know its MIME type. The easiest way to get the MIME type of the file is to go the media manager and click on “edit” for a file of the type in question. After you click “edit” you will see a screen like this:

Find the “File type:” and copy the text to it’s right, in the example above it is “audio/mpeg”, now that we have that string we can move on to the next step.

Generating the Media List

First we need to make some markup for the list to reside in, then we will populate an array by calling the get_children() function making sure to specify the post_type as “attachment” and the post_mime_type as “audio/mpeg” just like i saw it in the media manager under “file type:”

Finally in our loop we will call the wp_get_attachment_link() function with the current attachments ID to print a link to the file in question. It’s actually pretty simply once you see it all in action as below:

<ul>
<?php
$audios =& get_children( 'post_type=attachment&post_mime_type=audio/mpeg' );
foreach ( (array) $audios as $attachment_id => $attachment ) { ?>
<li><?php echo wp_get_attachment_link( $attachment_id ); ?></li>
<?php } ?>
</ul>

7 Responses to List Files In WordPress’ Media Library

  1. wes says:

    hey, great post. kind of a simplified version of the WordPress documentation!

    quick question though – I’ve been trying to do the same thing, but query all the media from the library (even if they’re not associated with any page/post). any thoughts?

    I’m currently resorting to straight PHP and MySQL queries, but if you think of anything, please let me know =)

  2. wes says:

    temporary solution success (broken up for readability):

    $all_media = $wpdb->get_results(
    “SELECT DISTINCT * FROM $wpdb->posts ” .
    “WHERE post_type = ‘attachment’ ORDER ” .
    “BY post_date DESC”
    );

    print_r( $all_media );

    =)

  3. Anthony says:

    Hey!

    Thanks for a great tutorial…I was desperately trying to figure out how to do this with Jpg’s and this has been perfect!

    Thanks again : )

  4. wp-wizard says:

    Just came across this today – go figure.

    Easy way to list all attachments using get_posts like so:

    'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_mime_type' => 'application/pdf',
    'post_parent' => null, // any parent
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $post) {
    setup_postdata($post); ?>
    Form Letters

    ID, false);
    echo '';
    }
    }

    Note that using get_posts you can use any column within the wp_posts table as a parameter, hence the "post_mime_type" argument.

  5. wp-wizard says:

    See if this works better:

    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_mime_type' => 'application/pdf',
    'post_parent' => null, // any parent
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $post) {
    setup_postdata($post); ?>
    Form Letters

    the_attachment_link($post->ID, false);
    echo '';
    }
    }

  6. Andrew Joseph says:

    Hmm that was weird, my comment got eaten. Anyway I desired to say that it is good to realize that someone else also mentioned this as I had trouble finding the exact same info elsewhere. This was the very first location that told me the answer. Thanks.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>