I'm trying to list events (custom posttype 'kurs') by event date, which are stored as custom fields ('dato').

My loop so far looks like this:

<?php $loop = new WP_Query( array( 'post_type' => 'kurs' ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<li><?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a>' ); ?></li>

<?php endwhile; ?>

What I need is a list of post(event)-titles from today forward in the future...

  • this has been asked like 345345325932845 times. simply store the date as a UNIX timestamp and use the orderby => 'meta_value_num' argument... Jan 25 '11 at 14:21

You need to use the meta_key to sort your events in your array. Like so:

<?php $loop = new WP_Query( array( 'post_type' => 'kurs', 'meta_key' => 'dato', 'order_by' => 'meta_value', 'order' => 'ASC' ) ); ?>
  • Cool, that works, thanks for the fast reply! Only one more thing: how to display just future events? Jan 25 '11 at 14:27
  • Do you mean that after the date is passed you want them to not be there anymore? The best way would be to use post expiration that sets a published event to "draft" or "trash". I use this plugin wordpress.org/extend/plugins/post-expirator . Also, since I did correctly answer your original question, mark it as correct so it shows up as answered. Jan 25 '11 at 14:32
  • This is what I mean, but I would like to use the already existing event date for this, otherwise I have to enter both event date and expiration date... Jan 25 '11 at 14:46
  • Not sure how to do that, I would post a new question. Jan 25 '11 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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