Last active
June 6, 2021 16:18
-
-
Save Jany-M/bff817d1b0b1dbeb569813f5ddc31076 to your computer and use it in GitHub Desktop.
[WP][WooCommerce] Add featured products filter
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 to show featured products, in admin panel for WooCommerce | |
function woocommerce_filter_products_by_featured_status() { | |
global $typenow, $wp_query; | |
if ($typenow=='product') : | |
// Featured/ Not Featured | |
$output .= "<select name='featured_status' id='dropdown_featured_status'>"; | |
$output .= '<option value="">'.__( 'Show All Featured Statuses', 'woocommerce' ).'</option>'; | |
$output .="<option value='featured' "; | |
if ( isset( $_GET['featured_status'] ) ) $output .= selected('featured', $_GET['featured_status'], false); | |
$output .=">".__( 'Featured', 'woocommerce' )."</option>"; | |
$output .="<option value='normal' "; | |
if ( isset( $_GET['featured_status'] ) ) $output .= selected('normal', $_GET['featured_status'], false); | |
$output .=">".__( 'Not Featured', 'woocommerce' )."</option>"; | |
$output .="</select>"; | |
echo $output; | |
endif; | |
} | |
add_action('restrict_manage_posts', 'woocommerce_filter_products_by_featured_status'); | |
function woocommerce_featured_products_admin_filter_query( $query ) { | |
global $typenow; | |
if ( $typenow == 'product' ) { | |
// Subtypes | |
if ( ! empty( $_GET['featured_status'] ) ) { | |
if ( $_GET['featured_status'] == 'featured' ) { | |
$query->query_vars['tax_query'][] = array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'slug', | |
'terms' => 'featured', | |
); | |
} elseif ( $_GET['featured_status'] == 'normal' ) { | |
$query->query_vars['tax_query'][] = array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'slug', | |
'terms' => 'featured', | |
'operator' => 'NOT IN', | |
); | |
} | |
} | |
} | |
} | |
add_filter( 'parse_query', 'woocommerce_featured_products_admin_filter_query'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment