Last active
January 3, 2016 12:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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