While trying to reproduce the solution found here (Sort products by brand and title in Woocommerce) by modifying it, I came across a bone ...
The first sort by brand works like a charm however I cannot find a way to do a second sort by increasing or decreasing price.
add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2);
function posts_clauses_with_tax( $clauses, $wp_query ) {
global $wpdb;
$taxonomies = array('pwb-brand');
$orderBy['field'] = "pwb-brand";
$orderBy['direction'] = "ASC";
if( in_array($orderBy['field'], $taxonomies) ) {
$clauses['join'] .= "
LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
";
$clauses['where'] .= " AND (taxonomy = '".$orderBy['field']."' OR taxonomy IS NULL)";
$clauses['groupby'] = "rel2.object_id";
$clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.slug ORDER BY slug ASC) ";
$clauses['orderby'] .= ", {$wpdb->posts}.post_title ASC";
return $clauses;
}
else {
return $clauses;
}
}
I've found those lines but it's very hard for me to add those lines in the query above to replace the orderby title by price ASC
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} price ON( {$wpdb->posts }.ID = price.post_id AND price.meta_key = 'price') ";
$clauses['orderby'] = " CONVERT( REPLACE(price.meta_value, '$', ''), DECIMAL(13,2) ) " . $order;
Do you have any idea of the solution?