Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to Sort Events by Next Upcoming Event WordPress

  • SOLVED

On the front page of this site I'm developing in Wordpress, I have a section of the page where I want the next 2 upcoming events (in the future) to be listed and sorted with the next upcoming event on top, and then the next upcoming event (after the most recent upcoming event) positioned below it. (See Screenshot for current coding results...)

So far, I have this piece of code, but I can't figure out how to sort it by next upcoming event, regardless of when the admin publishes the event in the WP Admin.

<?php
//The Query
query_posts('showposts=2&orderby=meta_value&meta_key=event_date&order=ASC&cat=4');
//The Loop
$count = 0;
while ( have_posts() ) : the_post(); $count++;
//The Title
$title = $post->post_title;
$event_datee = get_post_meta($post->ID, 'event_date', true);
?>
<dl>
<dt><a title="<?php echo $title; ?>" href="<?php the_permalink(); ?>"><?php echo $title; ?></a></dt>
<div class="postdate">
<div class="month m-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('n', $timestamp); //month ?>"></div>
<div class="day d-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('j', $timestamp); //day ?>"></div>
<div class="year y-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('Y', $timestamp); //year ?>"></div>
</div>
<dd><?php the_excerpt(); ?></dd>
</dl>
<?php endwhile; ?>



<strong>
Thanks for your help!</strong>

Answers (6)

2010-11-30

rilwis answers:

Can you try this query:

query_posts(
'cat' => 4
'posts_per_page' => 2,
'meta_key' => 'event_date',
'meta_value' => "'" . date('l, F d, Y') . "'",
'meta_compare' => '>',
'order_by' => 'meta_value',
'order' => 'ASC'
);


Spencer Barfuss comments:

Using that code totally broke the theme, creating nothing but a blank screen.


rilwis comments:

Sorry, that was typing mistake. Here's the correct code:

query_posts(array(
'posts_per_page' => 2,
'meta_key' => 'event_date',
'meta_value' => strftime("%Y-%m-%d", time()),
'meta_compare' => '>',
'order_by' => 'meta_value',
'order' => 'ASC'
));


Spencer Barfuss comments:

Alright, I think we've almost got it with your code, but it still is not sorting dates by next upcoming date relative to today's date, with the next upcoming date showing at the top. I entered in 3 new events: one on December 1st, one on December 2nd, and one on December 3rd. For some reason, the December 1st date is not even showing on there, but the events on December 2nd and 3rd are... weird.

(See attachment for screenshot...)

We're close to the solution, I think!