In WooCommerce backend on the productlist, we have the possibility to make a product featured by clicking on the star.
However, if you have a lot of products and you want to see afterwards which products are featured, we have to search through all products.
So I would like to make this column sortable so that all featured products are shown at the front or back during sorting
With my code I have so far, the column can be sorted, but the result becomes empty during sorting, resulting in 0 results
// 'featured' column sortable on the 'product' page
function sc_sortable_column( $columns ) {
$columns['featured'] = 'featured';
return $columns;
}
add_filter( 'manage_edit-product_sortable_columns', 'sc_sortable_column' );
// Orderby 'featured' (product)
function filter_pre_get_posts( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get('orderby');
// featured, product
if( $orderby == 'featured') {
$query->set( 'meta_key', '_featured' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'filter_pre_get_posts' );