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.
2 s
Copied from StackOverflow:
WP_Query
offers adate_query
parameter, allowing you to set a range with before and after.
$args = array(
'date_query' => array(
array(
'after' => 'January 1st, 2015',
'before' => 'December 31st, 2015',
'inclusive' => true,
),
),
);
$query = new WP_Query( $args );
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.