0

I'm trying to stuff a variable called $work_ids with the ID of any attached MP3 files for use in a custom player (based on MediaElement.js if that matters). The library contains WAV and other versions of each file which we don't want to stream, so I'm trying to filter those files, and only present MP3s in the variable (there can be more than one, that's fine). Here's what I've tried (and failed):

I thought this would get me rolling by itself:

$work_ids = get_attached_media( 'audio/mp3', $post->ID );

Nope, that returns nothing usable to the script.

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!), like $submission_attachment_ids, for whatever that's worth (just confirms my template CAN work with a variable I suppose). By itself, that variable, or any containing media attachment IDs, calls and runs the custom player and works fine.

How can I filter off the WAVs, and return only MP3s as IDs? I just want to filter filetype.

  • Problem solved half way at least... – Dave Davis Oct 18 '15 at 16:04
  • It would help to know what is usable by your script. – s_ha_dum Oct 18 '15 at 17:49
0

So here's what I did to resolve this...

I inserted the following code right after the loop started:

<?php 
    global $post;
    if( is_singular() && is_main_query()  && $post->post_type === 'download' ) {
        $download = new EDD_Download( $post->ID );
        $files = $download->get_files( );
        $submission_attachment_ids = '';
        if ( is_array( $files ) ){
            $first = true;
            foreach ( $files as $key => $value ) {
                if ( !isset( $value[ 'attachment_id' ] ) ){
                    continue;
                }
                if ( $first ){
                    $submission_attachment_ids .= $value[ 'attachment_id' ];
                    $first = false;
                } else {
                    $submission_attachment_ids .= ',' . $value[ 'attachment_id' ];
                }
            }
        } else { 
            die; // no files attached to the download
        }    

        if ( !$submission_attachment_ids || strlen( $submission_attachment_ids ) < 1 ) {
            return die; // no files attached to the download
        };

?>

Then, just above the content block I inserted my player call, which drops it right under the header:

<div id="player" width="100%">  
    <?php echo do_shortcode('[playlist captions="false" artists="false" images="false" ids="'.$submission_attachment_ids.'"]');?>
    </div>

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.