I was attempting to set the following code in the elementor/query/{query_id}
hook. The meta fields were created using ACF.
$meta_query = (array) $query->get( 'meta_query' );
$date_now = date( 'Y-m-d H:i:s' );
$meta_query[] = array(
'key' => 'event_startdate',
'compare' => '>=',
'value' => $date_now,
'type' => 'DATETIME',
);
$query->set( 'meta_query', $meta_query );
I tried both placing this code in the following function, with and without the code that sets the sorting (which works as expected)
add_action(
'elementor/query/sorted_by_date',
function( $query ) {
$query->set( 'meta_key', 'event_startdate' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
);
yet it does nothing. When I tried placing the same code inside the pre_get_posts hook it works just fine, and as expected.
I would love to understand what I'm not getting. As I see no reason it won't work in the elementor hook.
and also, where can I read about this set()
function? I tried in the documentation, but I didn't find any explanation for the odd behavior where, you first set the key of context, and only after that - the value itself. how does that work? (just like in the last piece of code)
many thanks