Created
March 16, 2024 10:10
-
-
Save tdmrhn/bc9b4fe0458c15c0585b96baa9ff3104 to your computer and use it in GitHub Desktop.
WooCommerce Show Featured Products First in Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pre_get_posts', function( $query ) { | |
if ( $query->is_main_query() && is_post_type_archive( 'product' ) ) { | |
$query->set( 'meta_query', array( | |
'relation' => 'OR', | |
array( 'key' => '_featured', 'value' => 'yes', 'compare' => 'EXISTS' ), | |
array( 'key' => '_featured', 'compare' => 'NOT EXISTS' ), | |
) ); | |
$query->set( 'orderby', array( '_featured' => 'DESC', 'date' => 'DESC' ) ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment