When i add this function for sorting featured products first, every other sortings works fine except this one and "Sort by last added products" (it shows same as sort by feautred product)...i found this function, but when i add new product that is not featured it shows it in first place, then i need to change product date to oldest one to show it in last place...is there any chance to make new product last in this sorting?
//// WOOCOMMERCE SORTING //////
add_filter( 'woocommerce_catalog_orderby', 'fa_remove_default_sorting_options' );
function fa_remove_default_sorting_options( $options ){
unset( $options[ 'popularity' ] );
//unset( $options[ 'menu_order' ] );
//unset( $options[ 'rating' ] );
//unset( $options[ 'date' ] );
//unset( $options[ 'price' ] );
//unset( $options[ 'price-desc' ] );
return $options;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_modified_date' );
function enable_catalog_ordering_by_modified_date( $args ) {
if ( isset( $_GET['orderby'] ) ) {
if ( 'featured' == $_GET['orderby'] ) {
return array(
'orderby' => 'featured',
'order' => 'DESC',
);
}
}
return $args;
}
add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_modified_date' );
function add_catalog_orderby_modified_date( $orderby_options ) {
// Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
return array(
'featured' => __("Sort by popularity", "woocommerce"),
) + $orderby_options ;
return $orderby_options ;
}
add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_modified_date' );
function default_catalog_orderby_modified_date( $default_orderby ) {
return 'featured';
}
add_action( 'woocommerce_product_query', 'product_query_by_modified_date' );
function product_query_by_modified_date( $q ) {
if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
$q->set( 'orderby', 'featured' );
$q->set( 'order', 'DESC' );
}
}
So here's the code that's working for me:
add_filter( 'woocommerce_catalog_orderby', 'fa_remove_default_sorting_options' );
function fa_remove_default_sorting_options( $options ){
unset( $options[ 'popularity' ] );
//unset( $options[ 'menu_order' ] );
//unset( $options[ 'rating' ] );
//unset( $options[ 'date' ] );
//unset( $options[ 'price' ] );
//unset( $options[ 'price-desc' ] );
return $options;
}
add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_modified_date' );
function add_catalog_orderby_modified_date( $orderby_options ) {
// Insert "Sort by modified date" and the clone of "menu_order" adding after others sorting options
return array(
'featured' => __("Sort by Featured", "woocommerce"),
) + $orderby_options ;
return $orderby_options ;
}
add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_modified_date' );
function default_catalog_orderby_modified_date( $default_orderby ) {
return 'featured';
}
function featured_products_orderby( $orderby, $query ) {
global $wpdb;
if ( 'featured' == $query->get( 'orderby' ) ) {
$featured_product_ids = (array) wc_get_featured_product_ids();
if ( count( $featured_product_ids ) ) {
$string_of_ids = '(' . implode( ',', $featured_product_ids ) . ')';
$orderby = "( {$wpdb->posts}.ID IN {$string_of_ids} ) " . $query->get( 'order' );
}
}
return $orderby;
}
add_filter( 'posts_orderby', 'featured_products_orderby', 10, 2 );
add_action( 'woocommerce_product_query', 'featured_woo_product_query' );
function featured_woo_product_query( $q ) {
if ( ! is_admin() && isset($_GET['orderby']) && 'featured' === esc_attr($_GET['orderby']) ) {
$q->set( 'orderby', 'featured' );
$q->set( 'order', 'DESC' );
}
}
Just to let you know, Woocommerce has moved the featured metadata to a taxonomy called product_visibility
. Hence your code isn't sorting by the meta featured
I've tested it, it works fine on my local site. Let me know if this works for you too.