add_filter('posts_clauses', 'sd_sort_by_season_priority', 20, 2);
function sd_sort_by_season_priority($clauses, $query)
{
if (
is_admin() ||
!$query->is_main_query() ||
!is_product_category('pokryshki')
) {
return $clauses;
}
global $wpdb;
$clauses['join'] .= "
LEFT JOIN {$wpdb->term_relationships} tr_season
ON ({$wpdb->posts}.ID = tr_season.object_id)
LEFT JOIN {$wpdb->term_taxonomy} tt_season
ON (tr_season.term_taxonomy_id = tt_season.term_taxonomy_id)
LEFT JOIN {$wpdb->terms} t_season
ON (tt_season.term_id = t_season.term_id)
";
$clauses['where'] .= "
AND (
tt_season.taxonomy = 'pa_season'
OR tt_season.taxonomy IS NULL
)
";
$clauses['orderby'] = "
CASE
WHEN t_season.slug = 'letnie' THEN 1
WHEN t_season.slug = 'vsesezonnye' THEN 2
WHEN t_season.slug = 'zimnie-neshipovannye' THEN 3
WHEN t_season.slug = 'zimnie-shipovannye' THEN 4
ELSE 5
END ASC,
{$wpdb->posts}.post_date DESC
";
return $clauses;
}