Support

Account

Home Forums General Issues Using datepicker field in a WP_Query Date Query

Solved

Using datepicker field in a WP_Query Date Query

  • http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

    I am making an event database. I want to make a WP_Query that gets posts that are in the future.

    For example:

    Event 1
    Event date (date picker field): 2014-01-01

    Event 2
    Event date (date picker field): 2018-01-01

    So I want a WP_Query that just gets Event 2, because it has not happened yet.

    Any example code available for this?

  • Well that turned out to be fairly easy. Here’s the code, hope it helps someone:

    To get items that have a date set in the future, make this WP_Query, where event_start_date is your custom field.

    
    $meta_query = array(
    	array(
    		'key' => 'event_start_date',
    		'value' => date('Ymd'),
    		'type' => 'DATE',
    		'compare' => '>='
    	)
    );
    
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => $posts_per_page,
    	'order' => 'DESC',
    	'orderby' => 'meta_value_num',
    	'meta_key' => 'event_start_date',
    	'offset' => (max(1, get_query_var('paged')) - 1) * $posts_per_page,
    	'meta_query' => $meta_query
    );
    
  • ok but what is the code to print the foreach?

    thank you

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Using datepicker field in a WP_Query Date Query’ is closed to new replies.