I want the products with the tag hot-sale to be displayed at the top of the product list on the Shop, Archive, Product Search Page, or any section where the product list is displayed, regardless of the sorting order, and then the other products to be displayed.
I have the following source code, but the problem is that it only displays products with the hot-sale tag and does not show other products:
function pre_get_posts_hot_product( $query ) {
if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) ) {
$hot_products_query = clone $query;
$hot_products_query->set( 'tax_query', array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'hot-sale',
'operator' => 'IN',
),
) );
$hot_products_query->set( 'fields', 'ids' );
$hot_products_ids = $hot_products_query->get_posts();
$query->set( 'post__not_in', $hot_products_ids );
$query->set( 'post__in', array_merge( $hot_products_ids, $query->get( 'post__not_in' ) ) );
$query->set( 'orderby', 'post__in' );
}
}
add_action( 'pre_get_posts', 'pre_get_posts_hot_product', 9999 );
1