In WordPress, I am trying to create an Elementor custom query to order products in Elementor loops.
The goal is to have products ordered by New products first, and then grouped by Color.
The fact that a product is labelled as New has to be controllable. Thus I created an ACF True / False field “statut_nouveaute”, so that when checked, a product is labelled as “New”.
Then the color grouping is done by another ACF field “couleur_tri” which is a simple text field, that I want to use to sort products by their color.
So the primary order is statut_nouveaute (true first) and then couleur_tri (A to Z).
I would like to be able to put “sort_by_color_and_nouveaute” in the “query ID” field in my Elementor Loop config so that my products are ordered that way.
I tried a number of other things but I can’t find a fully working solution
After searching a bit, here’s the code I made :
add_action( 'elementor/query/sort_by_color_and_nouveaute', function( $query ) {
$meta_query = array(
'relation' => 'AND',
'statut_nouveaute' => array(
'key' => 'statut_nouveaute',
'compare' => 'EXISTS',
),
'couleur_tri' => array(
'key' => 'couleur_tri',
'compare' => 'EXISTS',
),
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', array(
'statut_nouveaute' => 'DESC',
'couleur_tri' => 'ASC',
));
});
And that doesn’t work and I have no idea why.
The products are not sorted as they should.
It does have an influence on the order though, as if I change for example DESC to ASC, it changes the products on the screen.
I suspect that ACF True / False fields don’t add meta to the products when set to false (so it doesn’t exist at all). So maybe that’s one issue.
What am I doing wrong ?