0

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.

  • 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 Jun 7 '13 at 18:35
  • @Milo it is, but I don't know how to analyse it. Here's its output: pastebin.com/3er1WBMv – That Brazilian Guy Jun 8 '13 at 18:38
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

  • 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. – That Brazilian Guy Jun 8 '13 at 5:08
  • Did you try the code I posted? – s_ha_dum Jun 8 '13 at 12:18
  • Well, no, because your code expects dates to be in unix time, I supposed (incorrectly?). I'll try it now. – That Brazilian Guy Jun 8 '13 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 Jun 8 '13 at 20:07
  • 1
    That first underscore prevents the fields from showing up in the "Custom Meta" dropdown select box. – s_ha_dum Jun 8 '13 at 22:44
1

Copied from StackOverflow:

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

https://codex.wordpress.org/Class_Reference/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 );

The above suggestion of using a postmeta query is really out of date, no one should do that now that WP_Query supports these date queries.

  • Can you please add the WP version where this support has been added? – That Brazilian Guy Apr 23 at 20:43
  • 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 Apr 23 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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