Skip to content

Instantly share code, notes, and snippets.

@felipeelia
Last active August 3, 2023 13:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeelia/1214cede99a9bf27df68db3086dabf56 to your computer and use it in GitHub Desktop.
Save felipeelia/1214cede99a9bf27df68db3086dabf56 to your computer and use it in GitHub Desktop.
Bring featured products first in a WP_Query
@sonnv25
Copy link

sonnv25 commented Nov 26, 2020

Great! Thank you very much.

@Just-ars
Copy link

Just-ars commented Nov 29, 2020

function featured_products_orderby( $orderby, $query ) {
global $wpdb;

if ( 'featured_products' == $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' )." , post_date DESC";
}
}

return $orderby;
}
add_filter( 'posts_orderby', 'featured_products_orderby', 10, 2 );

Thank you!
I add sort products by date.

@pettrushkov
Copy link

Thank you, guys, it works excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment