2

WooCommerce has a simple way to pick "Featured" products by clicking a little star icon in the "Featured" column in the admin area.

I need to be able to filter and show only featured products in the admin area. Does anyone know of a straightforward way to do this?

I did some digging and it turns out this is done with meta key/values, where _featured is the key and the value is either yes or no.

Unfortunately, since it's not done with taxonomies like product categories, there is no way to use the filter functionality in the admin area to show only featured products. You can sort the featured column so they are all either at the top of bottom of the product list, but that's it.

1

3 Answers 3

13

You can sort your products list by the featured status. Just click on the star at the top of the column.... once will put all non-featured items at the top, and twice should reverse this and put all featured items at the top.

Your admin url will look like: /wp-admin/edit.php?post_type=product&orderby=featured&order=desc

Sort products by featured status

EDIT:

Ok, so it turned out to not be that hard to filter by featured status. WooCommerce is filtering by "subtype", and since that is also filtering by meta, we can mostly just copy their code and make a few adjustments.

The first function adds the select/drop-down element, while the second handles the adjustments to the admin query.

/**
 * Filter products by type
 *
 * @access public
 * @return void
 */
function wpa104537_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', 'wpa104537_filter_products_by_featured_status');

/**
 * Filter the products in admin based on options
 *
 * @access public
 * @param mixed $query
 * @return void
 */
function wpa104537_featured_products_admin_filter_query( $query ) {
    global $typenow, $wp_query;

    if ( $typenow == 'product' ) {

        // Subtypes
        if ( ! empty( $_GET['featured_status'] ) ) {
            if ( $_GET['featured_status'] == 'featured' ) {
                $query->query_vars['meta_value']    = 'yes';
                $query->query_vars['meta_key']      = '_featured';
            } elseif ( $_GET['featured_status'] == 'normal' ) {
                $query->query_vars['meta_value']    = 'no';
                $query->query_vars['meta_key']      = '_featured';
            }
        }

    }

}

add_filter( 'parse_query', 'wpa104537_featured_products_admin_filter_query' );

Updated for WooCommerce 3.0

The parse_query callback must be updated as the featured status is no longer stored as post meta.

/**
 * Filter the products in admin based on options
 *
 * @access public
 * @param mixed $query
 * @return void
 */
function wpa104537_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', 'wpa104537_featured_products_admin_filter_query' );
9
  • Thank you. As I noted in the last sentence of my question, I am aware of that sorting function. However, I want to use the Sort Products feature, which allows drag and drop sorting, to sort Featured Products, which is not possible by default. You can filter by category, then hit "Sort Products" and drag them into the order you want. I want this for featured products... Jun 28, 2013 at 19:04
  • I was just reading an article today about how people don't read the entirety of articles on the web, looks like you caught me. Check my edit. Jun 28, 2013 at 20:08
  • Looks like I was finding my solution as you were posting yours, and they look similar. Thanks! Jun 28, 2013 at 20:26
  • 2
    Well it is 4 years old and WooCommerce underwent a massive overhaul with it's 3.0 release. I've updated and it seems to work. May 26, 2017 at 20:21
  • 1
    your original code helped me create a filter by meta value. thanks.
    – dw1
    Jun 19, 2019 at 14:34
1

I found this plugin: https://wordpress.stackexchange.com/a/45447/15190, which I was able to customize to provide a select list to filter by Featured product. With that, I am able to then use WooCommerce's Sort Products drag and drop feature to easily change the order that Featured products appear in the Featured Products widget and other places.

Here is the code with my customizations that will make it work for WooCommerce Featured Products:

<?php
/*
Plugin Name: Admin Filter By WooCommerce Featured Products
Plugin URI: http://en.bainternet.info
Description: adapted from https://wordpress.stackexchange.com/q/45436/2487. Allows you to show only Featured products, which will then allow for drag and drop sorting of Featured products
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/

add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
 * First create the dropdown
 * 
 * @author Ohad Raz
 * 
 * @return void
 */
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }

    //only add filter to post type you want
    if ('product' == $type){
        //change this to the list of values you want to show
        //in 'label' => 'value' format
        $values = array(
            'Show Only Featured' => 'Yes', 
            'Show Only Non-Featured' => 'No',
        );
        ?>
        <select name="Featured">
        <option value=""><?php _e('Show Featured & Non-Featured', 'wpse45436'); ?></option>
        <?php
            $current_v = isset($_GET['Featured'])? $_GET['Featured']:'';
            foreach ($values as $label => $value) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }
}

add_filter( 'parse_query', 'wpse45436_posts_filter' );
/**
 * if submitted filter by post meta
 * 
 * @author Ohad Raz
 * @param  (wp_query object) $query
 * 
 * @return Void
 */
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Featured']) && $_GET['Featured'] != '') {
        $query->query_vars['meta_key'] = '_featured';
        $query->query_vars['meta_value'] = $_GET['Featured'];
    }
}
3
  • Don't forget to select one of our answers as the official answer to move this from the 'unanswered questions' pile. Jun 30, 2013 at 14:17
  • In wordpress(4.7.5) and woocommerce(3.0.7), your code doesn't work.
    – John Shang
    May 26, 2017 at 17:17
  • Outdated code, does not work WP 5 & WC 4.8. It ads filter input on top of products list, but does not filter.
    – D.A.H
    Dec 20, 2020 at 19:39
1

You can use Wordpress plug-in Filter Featured Products for WooCommerce.

2
  • Although not updated in a long time, it still works perfectly. Thanks Dec 9, 2022 at 11:28
  • 1
    I've updated the code from @helgatheviking with for those who don't want a plugin. Ask me or upvote, I can't post because I haven't enough reputation on this community Dec 9, 2022 at 11:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.