How can I list URLs of all audio files within my media gallery?

I have a music player in my theme that generates its playlist from a javascript file that looks like this

var myPlaylist = [
     {
        mp3:'track url goes here',
        title:'title here',
        artist:'artist',
    }
    {
        mp3:'track url goes here',
        title:'title here',
        artist:'artist',
    } 
etc...

];

How can I query the media library so it will echo the track url, title text and maybe an existing audio parameter like “description” for the artist value?

I know enough about php to put them in the right places once they’re queried, I just needto know how to pull them from the wp database!

2 Answers
2

Welcome to WPSE marctain!

Edit
There are some critiques on using the guid but no one of the commentators managed to edit this answer to a better one, so I’ll do it.

Yes, using guid is a bad idea in the long run, I knew that and I should have pointed that out, I didn’t, it was a mistake. I’m sorry, it was a quick and dirty answer.

In my original answer, I would have made usage of wp_get_attachment_url to get the correct url in any case. This adds an extra query, but it is safe to use.

$args = array
    (
        'post_type' => 'attachment',
        'post_mime_type' => 'audio',
        'numberposts' => -1
    );
 $audiofiles = get_posts($args);

 foreach ($audiofiles as $file)
 {
      $url = wp_get_attachment_url($file->ID);
      echo $url; // url
      echo file->post_title; //title
      echo file->post_content; // description
 }

Leave a Comment