Home › Forums › WoodMart support forum › How can I add "filter by brands" filter function in the backend? › Reply To: How can I add "filter by brands" filter function in the backend?
June 3, 2019 at 2:45 pm
#126970
metuza
Participant
Well as from what i can see in your code you are still using “add_action” which needs to be replaced with “add_filter”.
Give this code a trie, it is tested and works:
// Filter products by brand in WP Admin
add_action( 'restrict_manage_posts', 'aws_brand_products_sorting' );
function aws_brand_products_sorting() {
global $typenow;
$post_type = 'product'; // change to your post type
$taxonomy = 'pa_brand'; // change to your taxonomy
if ($typenow == $post_type) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
//$selected = isset($_GET[$info_taxonomy->name]) ? $_GET[$info_taxonomy->name] : '';
wp_dropdown_categories(array(
'show_option_all' => __("Show all {$info_taxonomy->label}", "aws-multi-functions"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_filter( 'parse_query', 'aws_brand_products_sorting_query' );
function aws_brand_products_sorting_query( $query ) {
global $pagenow;
$post_type = 'product'; // change to your post type
$taxonomy = 'pa_brand'; // change to your taxonomy
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
Brgds
Rune