获取 WordPress 媒体中所有文件的元数据
Get metadata of all files in the media in WordPress
我需要检索非图像文件的元数据,例如 word、excel 或 pdf 文档。当元数据附加到页面或 post 时,我能够检索到元数据,但当它未附加时,我什么也得不到。
我尝试使用 get_post_meta( $post_id );
和 wp_get_attachment_metadata( $attachment_id );
我在使用 get_post_meta( $post_id );
时只得到以下信息:
Array
(
[_wp_attached_file] => Array
(
[0] => 2015/03/test.xlsx
)
[_edit_lock] => Array
(
[0] => 1425404399:741
)
)
wp_get_attachment_metadata( $attachment_id );
没有。
您可以获得所有附件(无论它们是否附加到 post 或页面):
$attachments = get_posts( array('post_type' => 'attachment) );
制作一个你不想要的 mime 类型的数组:
$exclude = array(
'image/jpeg',
...
);
然后你可以通过它们查看 mime 类型:
foreach ($attachments as $attach) {
if (!in_array($attach->post_mime_type, $exclude) {
// Do what you want to do with it
}
}
wp_get_attachment_metadata 似乎没有 return 除图像(我猜是视频)以外的文件的元数据。您仍然可以插入描述和替代文本,例如媒体上传器中的 PDF 文件。我假设您指的是这些字段。
Wordpress 将这些值存储到 post_content 和 post_excerpt 因此如果您知道附件的附件 ID (post id) 那么您可以查询 post并打印元数据:
$pdf = get_post($attachment_id);
echo $pdf->post_content;
echo $pdf->post_excerpt;
我需要检索非图像文件的元数据,例如 word、excel 或 pdf 文档。当元数据附加到页面或 post 时,我能够检索到元数据,但当它未附加时,我什么也得不到。
我尝试使用 get_post_meta( $post_id );
和 wp_get_attachment_metadata( $attachment_id );
我在使用 get_post_meta( $post_id );
时只得到以下信息:
Array
(
[_wp_attached_file] => Array
(
[0] => 2015/03/test.xlsx
)
[_edit_lock] => Array
(
[0] => 1425404399:741
)
)
wp_get_attachment_metadata( $attachment_id );
没有。
您可以获得所有附件(无论它们是否附加到 post 或页面):
$attachments = get_posts( array('post_type' => 'attachment) );
制作一个你不想要的 mime 类型的数组:
$exclude = array(
'image/jpeg',
...
);
然后你可以通过它们查看 mime 类型:
foreach ($attachments as $attach) {
if (!in_array($attach->post_mime_type, $exclude) {
// Do what you want to do with it
}
}
wp_get_attachment_metadata 似乎没有 return 除图像(我猜是视频)以外的文件的元数据。您仍然可以插入描述和替代文本,例如媒体上传器中的 PDF 文件。我假设您指的是这些字段。
Wordpress 将这些值存储到 post_content 和 post_excerpt 因此如果您知道附件的附件 ID (post id) 那么您可以查询 post并打印元数据:
$pdf = get_post($attachment_id);
echo $pdf->post_content;
echo $pdf->post_excerpt;