WordPress有两种不同的媒体显示方式。有媒体覆盖/模式视图,还有列表视图,导航到管理时会触发该视图>;媒体(&T);然后单击过滤器选择框旁边的列表视图图标。要过滤两个位置的附件,我们需要使用两个单独的挂钩。
Filtering attachments in the Media Library (modal view)
这个
ajax_query_attachments_args
过滤器可用于调整用于在媒体覆盖屏幕上获取附件的查询。(改编自
this answer 来自birgire。)
/**
* Hide attachment files from the Media Library\'s overlay (modal) view
* if they have a certain meta key set.
*
* @param array $args An array of query variables.
*/
add_filter( \'ajax_query_attachments_args\', \'wpse_hide_cv_media_overlay_view\' );
function wpse_hide_cv_media_overlay_view( $args ) {
// Bail if this is not the admin area.
if ( ! is_admin() ) {
return $args;
}
// Modify the query.
$args[\'meta_query\'] = [
[
\'key\' => \'rrhh-file\',
\'compare\' => \'NOT EXISTS\',
]
];
return $args;
}
Filtering attachments in the Media Library (list view)
我们需要一种额外的、单独的方法来修改列表视图(旧的列表模式,非模式视图)中媒体库屏幕上的附件查询。这个
pre_get_posts
胡克会成功的:
/**
* Hide attachment files from the Media Library\'s list view
* if they have a certain meta key set.
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
add_action( \'pre_get_posts\', \'wpse_hide_cv_media_list_view\' );
function wpse_hide_cv_media_list_view( $query ) {
// Bail if this is not the admin area.
if ( ! is_admin() ) {
return;
}
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Only proceed if this the attachment upload screen.
$screen = get_current_screen();
if ( ! $screen || \'upload\' !== $screen->id || \'attachment\' !== $screen->post_type ) {
return;
}
// Modify the query.
$query->set( \'meta_query\', [
[
\'key\' => \'rrhh-file\',
\'compare\' => \'NOT EXISTS\',
]
] );
return;
}
在这两种情况下,我们都设置了一个元查询,以排除任何具有元键的附件
rrhh-file
设置您可以更改元查询或条件以满足您的需要。
请注意,元查询速度很慢。另一种方法是,在上载/创建这些特殊媒体项目时,将其分配给自定义分类法,然后更改查询以使用税务查询排除这些项目。
Edit: Debugging info
我使用以下功能可以更轻松地编辑自定义附件字段:
/**
* Add custom field to media
*/
add_filter( \'attachment_fields_to_edit\', \'wpse_cv_attachment_fields\', 10, 2 );
function wpse_cv_attachment_fields( $fields, $post ) {
$meta = get_post_meta( $post->ID, \'rrhh-file\', true );
$fields[\'rrhh-file\'] = array(
\'label\' => __( \'RRHH File\', \'text-domain\' ),
\'input\' => \'text\',
\'value\' => $meta,
\'show_in_edit\' => true,
);
return $fields;
}
/**
* Update custom field within media overlay (via ajax)
*/
add_action( \'wp_ajax_save-attachment-compat\', \'wpse_cv_media_fields\', 0, 1 );
function wpse_cv_media_fields() {
$post_id = $_POST[\'id\'];
$value = isset( $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] ) ? $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] : false;
if ( \'1\' === $value ) {
update_post_meta( $post_id, \'rrhh-file\', $value );
} else {
delete_post_meta( $post_id, \'rrhh-file\' );
}
clean_post_cache( $post_id );
}
/**
* Update media custom field from edit media page (non ajax).
*/
add_action( \'edit_attachment\', \'wpse_cv_update_attachment_meta\', 1 );
function wpse_cv_update_attachment_meta( $post_id ) {
$value = isset( $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] ) ? $_POST[\'attachments\'][ $post_id ][\'rrhh-file\'] : false;
//exit ( var_dump( $value ) );
if ( \'1\' === $value ) {
update_post_meta( $post_id, \'rrhh-file\', $value );
} else {
delete_post_meta( $post_id, \'rrhh-file\' );
}
return;
}
我使用以下SQL检查
rrhh-file
字段已设置:
SELECT * FROM wp_postmeta w where meta_key = \'rrhh-file\';
在列表模式下查看媒体库时,应受到
pre_get_posts
钩
$query->is_main_query()
正在返回
true
和
$screen
, 这个
WP_Screen
对象,如下所示:
WP_Screen Object
(
[action] =>
[base] => upload
[columns:WP_Screen:private] => 0
[id] => upload
[in_admin:protected] => site
[is_network] =>
[is_user] =>
[parent_base] =>
[parent_file] =>
[post_type] => attachment
[taxonomy] =>
[_help_tabs:WP_Screen:private] => Array
(
)
[_help_sidebar:WP_Screen:private] =>
[_screen_reader_content:WP_Screen:private] => Array
(
)
[_options:WP_Screen:private] => Array
(
)
[_show_screen_options:WP_Screen:private] =>
[_screen_settings:WP_Screen:private] =>
)
以及
$query
, WP\\U查询对象如下所示:
WP_Query Object
(
[query] => Array
(
[m] => 0
[cat] => 0
[post_type] => attachment
[posts_per_page] => 20
[post_status] => inherit,private
)
[query_vars] => Array
(
[m] => 0
[cat] => 0
[post_type] => attachment
[posts_per_page] => 20
[post_status] => inherit,private
[error] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] => 1
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => b01f0d78a3a985d46374d4384bba30b7
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)