Skip to content

Instantly share code, notes, and snippets.

@jdhobbsuk
Last active August 29, 2015 13:57
Show Gist options
  • Save jdhobbsuk/9835444 to your computer and use it in GitHub Desktop.
Save jdhobbsuk/9835444 to your computer and use it in GitHub Desktop.
WP_Query tweak for future events (eg. Events)
// the rest of your $args array
$now = date('Ymd');
$date_key = 'date';
$args['meta_query'] = array(
array(
'key' => $date_key,
'value' => $now,
'compare' => '>=',
),
);
// Set query to order by custom field
$args['meta_key'] = $date_key;
$args['orderby'] = 'meta_value_num';
// the WP_Query call
@jdhobbsuk
Copy link
Author

This 'meta_query' addition will ensure that the only posts returned are ones where a custom value (date key) is a date in the future.

This is useful when you've got a post type like "Events" or "Meetings" but only want to show the items that are happening in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment