I have created a filter dropdown in Media list view to filter media files based on selected term. The dropdown rendered OK with the custom taxonomy item. But the filter is not working at all. Irrespective of the term I select from this dropdown, all media items are being listed. No filtration is happening.
Here is my code to create the filter dropdown (in functions.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');
And this is the code I am using to for filtration purpose:
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');