I am trying to get a custom loop to work and I can not see why it is failing.

    $args = array( 'category_name' => 'audio', 'posts_per_page' => 6 ); 
    $sound = new WP_Query( $args ); 
    if( $sound->have_posts()): 

     while ( $sound->have_posts()) : $sound->the_post();
         $file = get_attached_media('audio'); 

        foreach( $file as $data) { ?>
            <li><a href="#" data-src="<?php echo $data->guid; ?>" class="tracks" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a></li>
        <?php } ?>

    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

Right now there is one post in the category. I had 6 but only 5 showed up on the page. So I deleted them all and started over. I made one post. Nothing shows. When I do a var dump of the $file variable, I get this



shouldn't there be a "post" object there? is my loop messed up?

4 Answers 4


Try to pass the Id of the post type.


 $file = get_attached_media('audio'); To
 $file = get_attached_media('audio',get_the_ID());


  • No joy there. Good suggestion though.
    – Jamie
    Sep 16, 2015 at 3:39
  • Could you try to remove the audio. And change the code to $file = get_attached_media('',get_the_ID()); var_dump()</pre> the new $file and check the results
    – bagpipper
    Sep 16, 2015 at 3:44
  • The loop does work. If I add the_content(); then I get what should be there. I attached an audio file to the post and I am trying to get just the url of the file. instead, the content returns an audio tag with all the information some of which I want.
    – Jamie
    Sep 17, 2015 at 1:39
  • @Jamie By attach do you mean put it in the editor?
    – bagpipper
    Sep 18, 2015 at 7:49
  • Also try to print_r() and check. If it still does not display anything check your attachment in the media section and see if it has been attached or not?
    – bagpipper
    Sep 18, 2015 at 8:02

As you said "Right now there is one post in the category". Are you sure you have attached a audio media for the post ? If yes,try this

1.Inside while loop var dump the post id. var_dump(get_the_ID); 2.If you get that correct then the problem is with audio media.


You can try showing the_title() in your loop query. To make sure your query is working :

while ( $sound->have_posts() ) : $sound->the_post();
    $file = get_attached_media( 'audio' ); 
    the_title(); // show the title post
        foreach( $file as $data ) { 
        ?><li><a href="#" data-src="<?php 
            echo $data->guid; ?>" class="tracks" title="<?php 
            the_title(); ?>"><?php the_post_thumbnail(); ?><?php 
            the_title(); ?></a></li><?php

If the_title() in post shows and audio doesn't show, make sure you have attached the audio file.


I figured it out. I used this inside the loop


to get the results I wanted. The audio url is stored in the wp_postmeta table. I opened it up and looked for the content that I was trying to get. I found it and it had the key of "enclosure". I don't know why it would have that key but it did. So I then could do this

 $audioUrl = strtok(get_post_meta(get_the_ID(), 'enclosure', true), "\n"); ?>

I used strtok() because for some reason, there were some other characters added to the url at the end. They were on a new line. I don't know what they mean. so what was returned was this

 string "http://example.com/audio.mp3

so the strtok function took of the last two lines and left me with a nice clean address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.