6

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
  • 1
    presumably it's being stored in post meta, use get_post_custom to see all of the metadata associated with an event, and a meta query to query based on the date key.
    – Milo
    Commented Jun 7, 2013 at 18:35
  • @Milo it is, but I don't know how to analyse it. Here's its output: pastebin.com/3er1WBMv Commented Jun 8, 2013 at 18:38

2 Answers 2

12

Copied from StackOverflow:

WP_Query offers a date_query parameter, allowing you to set a range with before and after.

https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

$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.

2
  • Can you please add the WP version where this support has been added? Commented Apr 23, 2019 at 20:43
  • 2
    As it says on the Codex article I linked date_query (array) - Date parameters (available since Version 3.7). so it's truly ancient at this point, definitely safe to use :)
    – jerclarke
    Commented Apr 23, 2019 at 20:59
3

It sounds like you need a meta_query.

$query = new WP_Query ( 
  array( 
    'paged' => $paged, 
    'post_type' => 'event', 
    'posts_per_page' => 4,
    'meta_query' => array(
      array(
        'key' => 'date_key_name',
        'value' => date('Y-m-d'),
        'compare' => '>=',
      )
    )
  )
);

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

15
  • the custom field is stored in the YYYY-MM-DD format. I've tried comparing using meta_key + meta_value, meta_query + key + value, '=' and 'LIKE' operators. Nothing seems to work. Even by inserting the literal event date. Commented Jun 8, 2013 at 5:08
  • Did you try the code I posted?
    – s_ha_dum
    Commented Jun 8, 2013 at 12:18
  • Well, no, because your code expects dates to be in unix time, I supposed (incorrectly?). I'll try it now. Commented Jun 8, 2013 at 18:39
  • 1
    @ruda.almeida aha, that's true, hadn't thought of that! though if you want to order by start date, that key has to be part of the query.
    – Milo
    Commented Jun 8, 2013 at 20:07
  • 1
    That first underscore prevents the fields from showing up in the "Custom Meta" dropdown select box.
    – s_ha_dum
    Commented Jun 8, 2013 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.