Juan Rangel
Juan Rangel

Reputation: 1793

Get attachment URL

I am attempting to grab the uploaded audio file from WordPress but having some issues. I need to grab the patch excluding the domain name so something like wp-content/uploads/2014/09/file.mp3.

I tried to use get_attached_media() and I can see exactly what I need when I var_dump() it is stored in an array with the key of guid. I have tried several different ways but I cannot access it. This is the result.

object(WP_Post)#2059 (24) {
 ["ID"]=>
 int(4312)
 ["post_author"]=>
 string(1) "3"
 ["post_date"]=>
 string(19) "2014-10-06 15:33:16"
 ["post_parent"]=>
 int(4298)
 ["guid"]=>
 string(73) "/wp-content/uploads/2014/09/file.mp3"

 }
}

I removed most of the code to keep it brief. How can I access the guid key? I store the results in $a and tried $a->guid $a['guid']and $a->post->guid and others but no luck.

Any help would be greatly appreciated.

Upvotes: 2

Views: 1835

Answers (1)

codeaken
codeaken

Reputation: 884

If you look at the documentation of the get_attached_media function you will see that it returns an array of WP_Post objects.

The array is indexed by attachment id so you can't access the first element simply by doing $a[0]. I would recommend that you reindex the array before using it, like this:

$a = get_attached_media(...);
$a = array_values($a);
echo 'GUID: ' . $a[0]->guid;

You could also iterate over all the attachments like this

foreach ($a as $attachment) {
    echo "GUID: {$attachment->guid}\n";    
}

Upvotes: 4

Related Questions