Created
May 1, 2024 11:31
-
-
Save WpComet/28a068157536334fbf68e55bcbe12013 to your computer and use it in GitHub Desktop.
Adding a 'featured' filter to WooCommerce products screen
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 a custom filter to the query | |
add_action('pre_get_posts', 'featured_products_admin_filter_query'); | |
function featured_products_admin_filter_query($query) { | |
global $pagenow; | |
if (is_admin() && $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'product' && isset($_GET['is_featured']) && $_GET['is_featured'] == '1') { | |
$query->set('tax_query', array( | |
array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'name', | |
'terms' => 'featured', | |
) | |
)); | |
} | |
} | |
// Add the custom filter to the admin page | |
add_action('restrict_manage_posts', 'featured_products_admin_filter'); | |
function featured_products_admin_filter() { | |
global $typenow; | |
if ($typenow == 'product') { | |
$filter_id = 'is_featured'; | |
$current = isset($_GET[$filter_id]) ? $_GET[$filter_id] : ''; | |
echo '<select name="' . $filter_id . '">'; | |
echo '<option value="">' . __('All products', 'woocommerce') . '</option>'; | |
echo '<option value="1" ' . selected($current, '1', false) . '>' . __('Featured', 'woocommerce') . '</option>'; | |
echo '</select>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment