I am trying to give users the ability to sort products on a store page by featured. I use the following code for this purpose:
add_filter( 'woocommerce_get_catalog_ordering_args', 'victor_get_catalog_ordering_args' );
function victor_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'featured' == $orderby_value ) {
$args['orderby'] = '_featured';
$args['order'] = 'DESC';
$args['meta_key'] = '_featured';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'victor_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'victor_catalog_orderby' );
function victor_catalog_orderby( $sortby ) {
$sortby['featured'] = 'Featured';
return $sortby;
}
But it doesn't work. I'm trying to sort products by featured using: /?orderby=featured But nothing is displayed, except for the message that no products were found for the request. But I know that in the admin panel I have more than 10 products marked as featured. Please help with this issue.I need to be sure to understand what I'm doing wrong.