I’m using the events manager plugin to create custom posts for events. I do not have a need for a blog, so I modified my theme’s index.php
file (using a child theme) for its query to retrieve the “event” post type on the home page.
new WP_Query ( array( 'paged' => $paged, 'post_type' => 'event', 'posts_per_page' => 4 )
(I have only included the ‘event’ post type).
So far, so good. But it displays current, past and future events on the home page. I want it to only display current and future events (that is, I want events that haven’t started yet to be shown on the home page too. I only want to prevent past events from being shown). I have taken a look at the codex page for WP_Query and used the following code excerpt:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date < CURDATE() ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' )
It doesn’t produce the desired result because the event date isn’t stored on post_date
on the DB.
However I suppose there’s some easy way to reference the events’ dates, because the date automatically displayed on the home page isn’t the posts publication date, but rather the event start date, without any modification being made.
Copied from StackOverflow:
See the linked documentation for more details.
Note that the other answer, which uses a postmeta query, is really out of date, no one should do that now that
WP_Query
supports these date queries.It sounds like you need a
meta_query
.Be aware that your dates need to be in some format that a machine can parse correctly.
Reference
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters