通常,我们可以通过以下方式过滤媒体>库中的图像:
日期,按MIME类型、附加/未附加文件。。。但我想将其扩展到按特征图像过滤。通过这种方式,我们可以只过滤那些设置为以某种帖子或自定义帖子类型为特色的图像。
你有什么想法使之成为可能吗?
谢谢
EDIT
我在函数中添加了此代码。php根据
this thread (它不会过滤,但应该将媒体库中的结果限制为那些具有特色的图像,即具有meta的\\u thumbnail\\u id),但管理员不会加载,服务器将删除一个错误。
add_action (\'pre_get_posts\', \'restrict_media\') ;
function
restrict_media ($query)
{
// get posts with thumbnails
$args = array (
\'post_type\' => \'galleries\',
\'post_status\' => array (\'publish\'),
\'posts_per_page\' => -1,
\'meta_query\' => array (
array (
\'key\' => \'_thumbnail_id\',
\'compare\' => \'EXISTS\',
),
),
) ;
$with_thumbnail = new WP_Query ($args) ;
// get the IDs of the thumbnails
$thumbnail_ids = array_map (function ($p) { return (get_post_meta ($p->ID, \'_thumbnail_id\', true)) ; },
$with_thumbnail->posts) ;
// include all thumbnails
$query->set (\'post__in\', array($thumbnail_ids)) ;
return ;
}
SO网友:birgire
我们可以通过以下方式将特色选项添加为假mime类型:
add_filter( \'media_view_settings\', function( $settings )
{
$settings[\'mimeTypes\'][\'wpsefeaturedimage\'] = \'Featured\';
return $settings;
});
它将显示如下:
然后我们可以使用
posts_where
筛选并检查我们的假mime类型:
/**
* Filter for featured images in the media library popup
*/
add_action( \'pre_get_posts\', function( \\WP_Query $q )
{
if( \'wpsefeaturedimage\' === $q->get( \'post_mime_type\' ) )
{
// Remove the fake mime type
$q->set( \'post_mime_type\', \'\' );
// Mark this query as featured filtered
$q->set( \'wpse_filter_featured\', true );
add_filter( \'posts_where\', function ( $where, \\WP_Query $q )
{
if( $q->get( \'wpse_filter_featured\' ) )
{
global $wpdb;
// Add \'featured images\' restriction to the SQL query
$where .= " AND {$wpdb->posts}.ID IN
( SELECT DISTINCT m.meta_value FROM {$wpdb->postmeta} m
WHERE m.meta_key = \'_thumbnail_id\'
) ";
}
return $where;
}, 10, 2 );
}
}, 1 );
我们可能可以进一步调整这一点。