Skip to content

Instantly share code, notes, and snippets.

@bjorn2404
Last active January 3, 2016 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjorn2404/8464594 to your computer and use it in GitHub Desktop.
Save bjorn2404/8464594 to your computer and use it in GitHub Desktop.
WordPress - get posts from beginning of time to 30 days in the future with custom meta date values. Useful for an events post type.
<?php
$upcoming_events = date( 'Ymd', strtotime( '+30 days' ) ); // Today + 30 days.
$events_args = array(
'posts_per_page' => 10,
'post_type' => 'event', //Events post type
'post_status' => 'publish,future', //Also includes scheduled posts
'orderby' => 'meta_value',
'meta_key' => 'start_date',
'order' => 'DESC',
'meta_query' => array(
array(
//Get all event posts that have a start date less than or equal to 30 days from now
'key' => 'start_date',
'value' => array( '00000101', $upcoming_events), //Beginning of time to 30 days in future
'type' => 'DATE',
'compare' => 'BETWEEN'
)
)
);
//Events query
$events_query = new WP_Query( $events_args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment