Hide custom posts after past certain date

137 views Asked by At

I have an Elementor site using ACF to create a custom post called coach-tour. Within the custom post I have created a date-picker field called trip_departure_date

I have these listed in a loop grid and ordered in date order (soonest first) using this PHP code:

add_action( 'elementor/query/trip_sorted_by_date', function( $query ) {

  $query->set( 'meta_key', 'trip_departure_date' );
  $query->set( 'orderby', 'meta_value_num' );
  $query->set( 'order', 'ASC' );

});

Is there a way to modify this code so that only posts that occur after todays date are displayed. i.e. when trip departure date is passed, those posts will be hidden.

1

There are 1 answers

2
Kristtof V88 On

Add condition :

$today = date('Ymd'); // Get today's date in the same format as your date picker

$meta_query = array(
    array(
        'key'     => 'trip_departure_date',
        'value'   => $today,
        'compare' => '>=', // Only show posts with departure date today or in the future
        'type'    => 'DATE' // Ensure the type is set to DATE for proper comparison
    )
);

$query->set( 'meta_query', $meta_query );