Skip to content

Instantly share code, notes, and snippets.

@ajmorris
Created December 12, 2017 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmorris/4970567ef2748e9d81d589ec18313622 to your computer and use it in GitHub Desktop.
Save ajmorris/4970567ef2748e9d81d589ec18313622 to your computer and use it in GitHub Desktop.
quick code snippet to filter featured products in WooCommerce.
<?php
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