我在媒体列表视图中创建了一个过滤器下拉列表,以根据所选术语过滤媒体文件。下拉列表使用自定义分类项呈现正常。但是过滤器根本不起作用。无论我从这个下拉列表中选择的术语如何列出所有媒体项目。没有过滤。
这是我创建过滤器下拉列表的代码(在funcitons.php中):
function media_add_content_category_filter_dropdown()
{
$scr = get_current_screen();
if ( $scr->base !== 'upload' ) return;
$terms = get_terms('media_content_category', array('hide_empty' => false));
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( 'mcfdd' ) );
print( '<option value="">All Categories</option>');
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->term_id ), esc_html( $term->name ) );
}
print( '</select>' );
}
}
add_action('restrict_manage_posts', 'media_add_content_category_filter_dropdown');
这是我用于过滤目的的代码:
function media_content_filter($query) {
if ( is_admin() && $query->is_main_query() ) {
if (isset($_GET['mcfdd']) && $_GET['mcfdd'] == -1) {
$query->set('mcfdd', '');
}
}
}
add_action('pre_get_posts','media_content_filter');
此时我卡住了,不知道该怎么办。有人请帮忙!
答案 0 :(得分:0)
在媒体库和过滤器上显示分类过滤器的工作解决方案:
//[[START] - Add custom taxonomy dropdown to media library
function media_add_content_category_filter_dropdown()
{
global $wp_query;
// check we're in the right place, otherwise return
if ( 'upload.php' != $pagenow )
return;
$tax_slug = 'media_content_category';
$tax_obj = get_taxonomy( $tax_slug );
// check if anything has been selected, else set selected to null
$selected = isset($wp_query->query[$tax_slug]) ? $wp_query->query[$tax_slug] : null;
wp_dropdown_categories( array(
'show_option_all' => __($tax_obj->label . ' - All'),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'name',
'selected' => $selected,
'hierarchical' => true,
// 'show_count' => true,
'hide_empty' => false
) );
}
add_action('restrict_manage_posts', 'media_add_content_category_filter_dropdown');
按自定义分类过滤帖子
function media_tsm_post_convert_id_to_term_in_query($query)
{
global $pagenow, $typenow;
// check we're in the right place, otherwise return
if ( 'upload.php' != $pagenow )
return;
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$var = &$query->query_vars[$tax_slug];
if ( $var != 0 ) {
$term = get_term_by( 'id', $var, $tax_slug );
$var = $term->slug;
}
}
}
add_filter('parse_query', 'media_tsm_post_convert_id_to_term_in_query');
//[END] - Add custom taxonomy dropdown to media library