WooCommerce products filter by tags within admin
Need the ability to filter the WooCommerce products list within admin by tags? Snippet of code below is for you. This concept can be edited for other purposes.
This can be easily changed to work in different scenarios.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function custom_woocommerce_filter_by_tag($output) { global $wp_query; $output .= wc_product_dropdown_categories(array( 'show_option_none' => 'Filter by product tag', 'taxonomy' => 'product_tag', 'name' => 'product_tag', 'selected' => isset( $wp_query->query_vars['product_tag'] ) ? $wp_query->query_vars['product_tag'] : '', )); return $output; } add_filter('woocommerce_product_filters', 'custom_woocommerce_filter_by_tag'); |
Found this snippet on the following website.
https://businessbloomer.com/woocommerce-display-custom-filters-wp-dashboard-products/
Dan Rickman
Hey Shane! Not sure if you’re monitoring this, but I’m struggling with an implementation of this. Quick question, if I were to use the code for a product attribute (in my example, artist), how would I get the “selected” to work? How would I target the attribute/term? What I have now:
add_filter( ‘woocommerce_product_filters’, ‘drick_filter_by_custom_taxonomy_dashboard_products’ );
function drick_filter_by_custom_taxonomy_dashboard_products( $output ) {
global $wp_query;
$output .= wc_product_dropdown_categories( array(
‘show_option_none’ => ‘Filter by artist’,
‘taxonomy’ => ‘pa_artist’,
‘name’ => ‘pa_artist’,
‘hide_empty’ => 0,
‘selected’ => isset( $wp_query->query_vars[‘pa_artist’] ) ? $wp_query->query_vars[‘pa_artist’] : ”, //THIS IS NOT WORKING
) );
return $output;
}
Maybe $wp_query->query_vars is not the right way? Appreciate any help you can provide.
Shane Rutter
I would probably just try using somthing along the lines of
'selected' => $_GET['pa_artist'] ?? '',