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 got posts that have starting date as a custom field value. I'm displaying few of those posts in my sidebar in ascending order so that the post with closest starting date is shown first. This is what I have and it works great:

<?php query_posts('&post_type=events&posts_per_page=5&meta_key=start_date&orderby=meta_value&order=asc'); ?><?php while ( have_posts() ) : the_post(); ?><li><span class="eventtext"><?php the_title(); ?></span>

But I was wondering, how could I filter the query so that it won't display posts with their starting dates already gone?

I've experimented with this:

$today = date('d/m/Y', strtotime('+2 hours'));
      query_posts(array(
      'post_type' => 'events',
      'posts_per_page' => 5,
      'meta_key' => 'start_date',
      'orderby' => 'meta_value',
      'order' => 'ASC',
      'meta_query' => array(
         array(
         'key' => 'start_date',
         'meta-value' => $value,
         'value' => 'start_date',
         'compare' => '>=',
         'type' => 'CHAR'
         )
      )
   ));

But it doesen't display anything. One thing to note is that the starting date value is saved in mm/dd/yyy -format, but changing the $today's date format haven't helped either.

UPDATE

So now I got this:

    <?php
$today = date('m/d/Y', strtotime('+2 hours'));
$the_query = new WP_Query( array(
      'post_type' => 'events',
      'posts_per_page' => 5,
      'meta_key' => 'start_date',
      'orderby' => 'meta_value',
      'order' => 'ASC',
      'meta_query' => array(
array(
         'key' => 'start_date',
'meta-value' => 'start_date',
         'value' => $today,
         'compare' => 'DATE',
         'type' => 'CHAR'
))
   ));
?>

Nothing shows up in the sidebar.

Note: the date is in that format since the posts are saved with the start_date custom field in that format. I'm just trying to get this work, then I'll change the date format to a more suitable one.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

For this to work reliably, the date format in the database should be yyyy-mm-dd. Comparison type should be DATE or NUMERIC. If you take the date of 25-04-2012 in present format and compare it numerically to the date 26-04-1986, you can see what the issue will be: 25042012 < 26041986

ALso- if you're doing these queries in addition to your main loop, use a new instance of WP_Query.

EDIT-

there were a few errors you didn't fix. note that this still won't work quite right until you fix the date format.

<?php
$today = date('m/d/Y', strtotime('+2 hours'));
$the_query = new WP_Query( array(
  'post_type' => 'events',
  'posts_per_page' => 5,
  'meta_key' => 'start_date',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
  array(
    'key' => 'start_date',
    'value' => $today,
    'compare' => '>=',
    'type' => 'DATE'
  ))
));
?>
share|improve this answer
    
Thank you for the suggestions, great points in using date or numeric instead of >. However, no matter if I use WP-Query or not, I can't get the posts to display. No errors or anything, the sidebar is just empty. Something wrong with my query? –  Andrew Apr 25 '12 at 14:55
    
@Andrew - in your meta_query, 'value' should be 'value' => $today. 'meta-value' isn't a valid parameter. –  Milo Apr 25 '12 at 15:11
    
Still no cigar. Simply nothing appears, very weird. –  Andrew Apr 25 '12 at 16:32
    
@Andrew - have you changed the date format in the database? –  Milo Apr 25 '12 at 16:33
    
@Andrew - update your question and add the new code you're currently using. –  Milo Apr 25 '12 at 16:41
show 4 more comments

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.