• Resolved gundigundi

    (@gundigundi)


    Hey guys, I′m having a hard time setting up a custom query.
    The goal is to display event-posts only for upcoming events. Setup is Elementor Pro combined with Astra Pro theme.
    I added a custom date field called ‘eventdatum’ (d/m/Y) to posts and via the Elementor Pro Posts widget I want to show only posts where that certain date is today or in the future. If the field is empty I do not want the post to be shown.
    Here is what I added to my functions.php:

    add_action( 'elementor/query/upcoming_events', function( $query ) {  
    $today = date('d/m/Y');    
    
    $query->set( 'post_type', 'post' );   
    $query->set( 'meta_query', array( array( 'key' => 'eventdatum', 'value' => $today, 'compare' => '>=' , 'type' => 'DATE' ) ) );   
    $query->set( 'meta_key', 'eventdatum' );   
    $query->set( 'orderby', 'meta_value_num' );   
    $query->set( 'order', 'ASC' );  } ); 

    After I added the query ID to the Posts widget it still shows all posts ever created. All the posts that actually have the custom field ‘event datum’ filled out show at the end of the list (ordered ascending by the ‘event datum’ field).
    What am I doing wrong?

    • This topic was modified 4 years, 1 month ago by gundigundi.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How do I correctly set up a WP-Query to only show upcoming event-posts?’ is closed to new replies.