Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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