I need to retrieve metadata of files that are not images such as word, excel or pdf documents. I was able to retrieve the metadata when it was attached to a page or post but when its not attached, not getting anything.
I tried using get_post_meta( $post_id );
and wp_get_attachment_metadata( $attachment_id );
I'm only getting the following information when using get_post_meta( $post_id );
:
Array
(
[_wp_attached_file] => Array
(
[0] => 2015/03/test.xlsx
)
[_edit_lock] => Array
(
[0] => 1425404399:741
)
)
Nothing for wp_get_attachment_metadata( $attachment_id );
.
You can get all the attachments (regardless of whether the are attached to a post or page) with:
$attachments = get_posts( array('post_type' => 'attachment) );
Make an array of the mime types you do not want:
$exclude = array(
'image/jpeg',
...
);
Then you can go through them looking at the mime type:
foreach ($attachments as $attach) {
if (!in_array($attach->post_mime_type, $exclude) {
// Do what you want to do with it
}
}
wp_get_attachment_metadata doesn't seem to return metadata for files other than images (and videos I guess). Still you are able to insert description and alternative text for e.g. PDF files in media uploader. I am assuming you are referring to those fields.
Wordpress stores those values to post_content and post_excerpt so if you know the attachment id (post id) of your attachment then you can just query that post and print metadata:
$pdf = get_post($attachment_id);
echo $pdf->post_content;
echo $pdf->post_excerpt;