Created
September 28, 2017 19:20
-
-
Save lukecav/5263416ca83aaf9b5f94cee4b1458285 to your computer and use it in GitHub Desktop.
Filter by featured products in wp-admin when using WooCommerce
This file contains 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
add_action('restrict_manage_posts', 'featured_products_sorting'); | |
function featured_products_sorting() { | |
global $typenow; | |
$post_type = 'product'; // change to your post type | |
$taxonomy = 'product_visibility'; // change to your taxonomy | |
if ($typenow == $post_type) { | |
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; | |
$info_taxonomy = get_taxonomy($taxonomy); | |
wp_dropdown_categories(array( | |
'show_option_all' => __("Show all {$info_taxonomy->label}"), | |
'taxonomy' => $taxonomy, | |
'name' => $taxonomy, | |
'orderby' => 'name', | |
'selected' => $selected, | |
'show_count' => true, | |
'hide_empty' => true, | |
)); | |
}; | |
} | |
add_filter('parse_query', 'featured_products_sorting_query'); | |
function featured_products_sorting_query($query) { | |
global $pagenow; | |
$post_type = 'product'; // change to your post type | |
$taxonomy = 'product_visibility'; // 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/46470206/woocommerce-filter-by-featured-products-in-admin/46476108#46476108