Show WooCommerce featured products first

mgibbs189

{{gist file=”functions.php” lang=”php”}}
<?php

add_filter( ‘posts_orderby’, function( $orderby, $query ) {
global $wpdb;

$post_types = (array) $query->get( ‘post_type’ );
if ( in_array( ‘product’, $post_types ) ) {
$featured_ids = wc_get_featured_product_ids();
if ( ! empty( $featured_ids ) ) {
$new_orderby = “FIELD(” . $wpdb->posts . “.ID,'” . implode( “‘,'”, $featured_ids ) . “‘) DESC”;
$orderby = empty( $orderby ) ? $new_orderby : “$new_orderby, $orderby”;
}
}
return $orderby;
}, 100, 2 );
{{/gist}}