get_posts sorting by event date using meta_compare
-
Here’s the situation – my blog is about upcoming, shall we say, events, and the date of the event is stored in a custom field in each post in the
M d, Y
format.What I am trying to do is make a custom page where these posts would be sorted by their custom field “event date” instead of post date. Also, I want to include only upcoming “events”. I am using
get_posts
for this purpose, like this:<?php $todaysDate = date('M d, Y'); $postslist = get_posts('numberposts=-1&order=ASC&meta_key=final&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value'); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br /> Event date: <?php echo get_post_meta($post->ID, 'final', true); ?></li> <?php endforeach; ?>
Everything’s fine, except it only gives me posts with dates from the current month.
…As I was writing this post I realized that’s probably because of the date format I’m using (
M d, Y
, as in Sep 3, 2011).Is there a way to solve this besides manually changing all the custom fields to a new date format?
And what date format should I use it would be able to compare event dates… Do I have to use Unix timestamp (like, 1315075580) or can it compare YYYY-MM-DD or whatever?
Edit: another problem, meta_compare seems to think that day 9 is more than day 30, so the sorting is all messed up.
- The topic ‘get_posts sorting by event date using meta_compare’ is closed to new replies.