0

I'm trying to write a piece of code that fetches the url/download link of any embedded audio files in the current post. In doing this I stumbled upon get_attached_media which outputted something like this (I have removed some of the values in order to keep it short):

Array (
    [172] => WP_Post Object ( 
        [ID]             => 172 
        [post_author]    => 1 
        [guid]           => http://localhost/wp-content/uploads/2013/07/03-take_me_out_128_lame_cbr_ex.mp3 
        [menu_order]     => 0 
        [post_type]      => attachment 
        [post_mime_type] => audio/mpeg 
    ) 
)

As you can tell it is all wrapped in this "WP_Post Object". Now, I'm sure this is all common place and fairly simple to resolve, but I can't figure out how. Essentially, what I want to do is to fetch the "GUID" value and attach it to a variable, as it seems to contain the URL of the post's embedded audio file.

Also, please correct my terminology so I can make this question more useful to others who migh seek an answer to this.

3
  • You normally access this way: $array[172]->guid - this is standard PHP. A more lengthy explanation (next to the PHP manual) is here: Able to see a variable in print_r()'s output, but not sure how to access it in code
    – hakre
    Commented Aug 19, 2013 at 22:46
  • Thanks! Let's just say I'm not very fluent in PHP. What could I substitute "172" with though? I need it to be the id of the currently fetched media/audio attachement. 172 is only specific for one of the posts.
    – gburning
    Commented Aug 19, 2013 at 23:03
  • It's already answered below, that is the ID of the post. Most things have got an ID due to the relational database system running in the background. guids came later and should identify in more open systems like across multiple, independet databases.
    – hakre
    Commented Aug 20, 2013 at 6:54

1 Answer 1

2

Don't use the GUID. Despite appearances, that isn't an URL. Use wp_get_attachment_image_src. Something like:

$image = wp_get_attachment_image_src($post_id,'full');
echo $image[0];

Or use wp_get_attachment_url with the post ID.

$image = wp_get_attachment_url($post_id);

Of course, I don't know what the actual variable name is that holds your images but you should see the ID in the post objects and should be able to work that out. But, for example...

$media = get_attached_media(null,1);
foreach($media as $m) {
  var_dump(wp_get_attachment_url($m->ID));
}
10
  • GUID is the only value that holds the URL and as I will not be changing the contents of it, merely copying them, there shouldn't really be any problems. Right? I can't use wp_get_attachement_image_src since I'm looking for any potential audio files embedded in the post contents, not images.
    – gburning
    Commented Aug 19, 2013 at 22:29
  • hmmm... maybe wp_get_attachment_url would be better. I would not use the GUID as it isn't really intended to be an URL and WordPress Core may change how that it handled at any time (and I wish it would to avoid confusion).
    – s_ha_dum
    Commented Aug 19, 2013 at 22:34
  • That could work! Only thing I need now is to get the id of the audio attachment.
    – gburning
    Commented Aug 19, 2013 at 22:45
  • You have the ID. It should be the IDs in the post objects.
    – s_ha_dum
    Commented Aug 19, 2013 at 22:46
  • 1
    If you used get_attached_media as in the question title then they are the IDs you need. WordPress treats attachments like a post type.
    – s_ha_dum
    Commented Aug 19, 2013 at 23:52

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.