• Resolved Mike

    (@manndtp)


    I am trying to get events to show with a start date either today or in the future. I have the following code:

    $today = getdate();
    $args = array(
    	'posts_per_page' => 15,
    	'post_type' => $type,
    	'post_status' => 'publish',
    	'meta_key'=>'event_start_date',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'date_query' => array(
    		'meta_key' => 'event_end_date',
    		'column' => 'event_end_date',
    		'after'  => array(
    			'meta_key' => 'event_end_date',
    			'year'  => $today['year'],
    			'month' => $today['mon'],
    			'day'   => $today['mday'],
    		),
    	'inclusive' => true,
    	)
    );

    Yet it shows nothing. Any help is appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The usual problem with date meta queries people have is the event date is stored as formatted date string. Queries work much better if the event date is stored as a timestamp.

    In any case, another problem you have is date queries do not work on meta data, only post dates. The column argument is for specifying which date column to use in the posts table only (there’s four of them).

    If you store event dates as timestamps, you merely need to do a meta query where the value is numerically greater than the current timestamp. Formatted date strings are very difficult to work with. It’s better to update the DB than try to work with date strings. IMHO of course.

    Thread Starter Mike

    (@manndtp)

    Wasn’t quite sure what to do with your response, but did a little bit more searching around and stumbled on this post from 2 years ago (used it as a guide):

    https://www.ads-software.com/support/topic/display-custom-post-type-by-custom-field-date-range?replies=9#post-2797408

    Reviewed the codex listed below and came up with this code that works:

    $args = array(
    	'posts_per_page' => 15,
    	'post_type' => $type,
    	'post_status' => 'publish',
    	'meta_key'=>'event_start_date',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'meta_query' => array(
    		array(
    			'value'         => date('Ymd',strtotime("today")),
    			'compare'       => '>=',
    			'type'          => 'DATE'
    		)
    	)
    );

    Reference: https://codex.www.ads-software.com/Class_Reference/WP_Query#Custom_Field_Parameters

    Thanks for your help to steer me in the right direction.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show events with start dates after today (Custom Post Type)’ is closed to new replies.