how to get individual songs from wordpress audio playlist?

Regolith asked
php wordpress audio playlist

I am trying to get songs name and url from a playlist that is created.

I have searched but all I can find is plugins for those playlists.

I want to split the audio files form the playlist and display only names of the songs in some part of my website.

I can display single audio file as

$music_file = get_template_directory_uri() . "/sounds/music.mp3"; 
echo do_shortcode('[audio mp3=' . $music_file . ']');

but I want to get all the audio files from playlist and loop through them individually and create a custom display of the playlist.


For example I can get individual images from gallery by using $gallery = get_post_gallery_images( $post ). Is there a function that can do the same for audio playlist.

Playlist is created from media upload in editor

enter image description here


In the source code for wp_playlist_shortcode() you will find the query that’s needed, which can be extracted and used to build a query of your own, as follows.

$playlist_attachments = get_children( [
    'post_status'    => 'inherit',
    'post_parent'    => get_the_ID(),

    'post_type'      => 'attachment', // Attachment post type.
    'post_mime_type' => 'audio', // Audio file attachments.
    'orderby'        => 'menu_order ID', // Playlist order.
    'order'          => 'ASC', // Ascending order.
] );

foreach ( $playlist_attachments as $id => $attachment ) {
    echo wp_get_attachment_link( $id ) . "n";

With that, you have the attachment ID and object for each of the audio files in the playlist. So you can get more creative if you’d like, and instead of calling wp_get_attachment_link(), consider using wp_get_attachment_url(). You may also find it helpful to review the source of wp_playlist_shortcode() and extract more, so you can tune things in just the way you’d like.

Share This
Posted in: