WordPress 文章查询教程12:如何使用 Mime Type 和返回字段相关参数
第12讲关于 Mime Type 和返回字段的参数:
Mime Type 参数
Mime Type 参数只可用于 attachment 文章类型:
post_mime_type(string/array) – 允许的 mime types.
获取 gif 图片
特别注意一下文章状态是 inherit.
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image/gif',
);
$query = new WP_Query( $args );
获取不是图片的附件
要排除某些 mime 类型,首先需要使用 get_allowed_mime_types() 函数获取所有的 mime 类型,然后使用 array_diff() 函数从允许的 mime 类型中去除所有图片的 mine 类型。
$unsupported_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
$all_mimes = get_allowed_mime_types();
$accepted_mimes = array_diff( $all_mimes, $unsupported_mimes );
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => $accepted_mimes,
);
$query = new WP_Query( $query_args );
返回字段参数
设置返回的字段。
fields(string) – 要返回的字段,有三个选项:'all'– 返回所有字段(默认值)。'ids'– 返回文章 IDs 数组。'id=>parent'– 返回含有 ID 和 post_parent 属性的标准对象数组。
传递任何其他内容都会返回所有字段(默认) – 就是文章对象数组。