Last active
August 27, 2024 08:51
-
-
Save FrancoStino/4b2c57896912e56294d8eb31d1844e8c to your computer and use it in GitHub Desktop.
Add a filter dropdown for specific product attribute in Woocommerce admin product list - Admin Page Product List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add a filter dropdown for specific product attribute in woocommerce admin product list - Admin Page Product List | |
*/ | |
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown'); | |
function product_attribute_sorting_dropdown() { | |
global $typenow; | |
$taxonomy = 'pa_fornitori'; | |
if ( $typenow == 'product' ) { | |
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; | |
$info_taxonomy = get_taxonomy($taxonomy); | |
wp_dropdown_categories(array( | |
'show_option_all' => $info_taxonomy->label, | |
'taxonomy' => $taxonomy, | |
'name' => $taxonomy, | |
'orderby' => 'name', | |
'selected' => $selected, | |
'show_count' => true, | |
'hide_empty' => true, | |
)); | |
}; | |
} | |
add_action('parse_query', 'product_attribute_sorting_query'); | |
function product_attribute_sorting_query( $query ) { | |
global $pagenow; | |
$taxonomy = 'pa_fornitori'; | |
$q_vars = &$query->query_vars; | |
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && 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; | |
} | |
} | |
/* --- */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment