Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 Aug 19 '13 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. –  burnso Aug 19 '13 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 Aug 20 '13 at 6:54
add comment

1 Answer 1

up vote 0 down vote accepted

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));
}
share|improve this answer
    
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. –  burnso Aug 19 '13 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 Aug 19 '13 at 22:34
    
That could work! Only thing I need now is to get the id of the audio attachment. –  burnso Aug 19 '13 at 22:45
    
You have the ID. It should be the IDs in the post objects. –  s_ha_dum Aug 19 '13 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 Aug 19 '13 at 23:52
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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