Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Dave Davis
Dave Davis
1,837 Points

How do I make get_attached_media retrieve ONLY mp3s and set a variable for use in a template?

I'm trying stuff a variable called $work_ids with the ID of any attached MP3 files for use in a player. The library contains wav and other versions we don't want to stream, so I'm trying to filter those files. Here's what I've tried (and failed):

I thought this would get me rolling by itself, if there's only one ID... $work_ids = get_attached_media( 'audio/mp3', $post->ID );

Nope.

So then I thought, maybe I need to throw in some commas to handle multiple mp3s returned. But nope, that's not the case... this works no better: $work_ids = join( ',', wp_list_pluck( get_attached_media('audio/mp3' ), 'ID' ) );

Next I figured it was the ID not banging, so I changed the reference to something more explicit: $work_ids = join( ',', wp_list_pluck( get_attached_media('audio/mp3' ), $post->ID) ); Another fail.

I put these right below the loop, where I draw a conventional WP playlist object [playlist ids=""] via shortcode. This part of the script actually works fine, when I insert a fixed ID, or use another variable floating around (which unfortunately includes wavs) $submission_attachment_ids['01']...

<?php echo do_shortcode('[playlist captions="false" artists="false" images="false" ids="'.$work_ids.'"]');?>

That calls and runs a custom player I made and works fine. This is making me nuts. I just want to filter filetype. argggggh!

2 Answers

Maybe just have it return every audio media, then filter out your returned object.

Dave Davis
Dave Davis
1,837 Points

Believe it or not, that's where i started. ;) It seemed overly complicated, but yeah, that may be the best solution. Unfortunately I have missed some key lesson... here's how I went about that:

I set an array up to get ONLY mp3 attachments, by filtering mime_type: $children = get_children(array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'audio/mp3' ));

I figured that would return the ID's of attachments. But no... I'm missing something there too. When I do a print_r of my returned variable in these cases, I get a very odd return - the Artist name, not the post ID or song title or anything more specific, which suggests the problem here is looking at "children" rather than attachments. So I tried this filter on the output... // $mime_types = get_allowed_mime_types(); // $remove_mime_types = array( // // Remove these formats from list // 'jpg|jpeg|jpe' => 'image/jpeg', // 'gif' => 'image/gif', // 'png' => 'image/png', // 'bmp' => 'image/bmp', // 'tif|tiff' => 'image/tiff', // 'ico' => 'image/x-icon' // 'wav|bwf|bwv' => 'audio/wav'

Again, nope. Can you point me to a reference or codex links required to 1)return media objects as IDs, 2) filter the ID array for MP3s, then 3)set a variable for all MP3 IDs to insert in my working do_shortcode. all these methods come from Stacks or Codex, I'm missing something here.

Dave Davis
Dave Davis
1,837 Points

I think I've got it sorted now. Thanks!