I have a custom post type called "Dates" for events. In that custom post type, I have an ACF date picker custom field called "date-event" which allows to put a date (formated "l j F", example: Monday 01 March) for an event.
I have included the elementor loop carousel and I need to ASC sort the loops by "date-event", including current date.
I know that elementor allows the use of custom query filters but my php coding knowledge is – to say the least – not great and I don’t really know how to do this. I’ve read a lot of answers on here but no luck.
For instance, I found this piece of code that I’ve tried to adapt but it doesn’t work:
function my_query_by_post_meta( $query ) {
$queried_object = get_queried_object();
$args = array(
'post_type' => 'dates',
'term' => $queried_object->slug,
'orderby' => 'meta_value_num',
'order' => ASC,
'meta_key' => 'date-event',
'meta_query' => array(
array(
'key' => 'date-event'
)
)
);
$query = new WP_Query($args);
}
add_action( 'elementor/query/{$query_id}', 'my_query_by_post_meta' );
Then I’ve tried this, without success as well :
function my_query_by_post_meta( $query ) {
return array(
'post_type' => 'dates',
'post_status' => 'publish',
'meta_key' => 'date-event',
'posts_per_page' => 15,
'orderby' => 'date-event',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date-event',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE'
),
)
);
}
add_action( 'elementor/query/{$query_id}', 'my_query_by_post_meta' );
Can you help me with this?
Thank you!
2
Answers
Somebody added an answer but, I don't know why, removed it afterwards.
It didn't work for me either but I'm pasting it here, just for reference :
Somebody helped me solve this issue. Here is the working piece of code :
Also, in the widget, make sure to select Source = the CPT (in this case, "Dates").
PS: I’ve tried to do this with the plugin Code Snippets but it wasn’t working. I’ve added it manually in functions.php and it was ok.