There are two methods:
-
$attachment_meta = get_post_meta( $post->ID, '_wp_attachment_meta', true );
Will return an
array()
:$width = $attachment_meta['width'];
That
true
as the third parameter is especially important in this case, since the metadata being queried is a serialized array. You need to return it as single, or you’re going to get an array returned, except the first item in that array will be the serialized array. Messy. Just passtrue
, and you’ll get your unserialized array returned. -
This function is sort of a wrapper for the above
get_post_meta()
call.$attachment_meta = wp_get_attachment_metadata( $post->ID );
In both cases, $post
is the attachment, not the parent post to which the attachment is uploaded.