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.
For this to work reliably, the date format in the database should be
yyyy-mm-dd
. Comparison type should beDATE
orNUMERIC
. 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 < 26041986ALso- 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.