0

I have a custom post type "event" which stores a custom meta as a timestamp ($event_date). The date is always in a dd-mm-yyyy format, so I can generate a unix timestamp from this key. The timestamp doesn't match the pubdate, it's just any date set in the future.

I'd like to make a wp query to list all upcoming posts (events) ie comparing present time with these timestamps and ordering the posts accordingly (show upcoming first, closer to present date). Pubdate should be disregarded; if date is ambiguous (if two events have same $event_date), then order them alphabetically or whatever.

I would like also to be able to query only the events occurring in the next 30 days.

I'm going to try this, but I'm wondering if there's a better way to do, because I don't know how to get only the posts scheduled within 30 days from now:

query_posts(array(
    'posts_per_page' => 30,
        'meta_key' => 'event_date',
    'meta_value' => date(Y-m-d), // I could use directly unix timestamps
    'meta_compare' => '>',
        'orderby' => 'meta_value',
        'order' => 'ASC'
));

this should sort the posts with the posts occurring in the future first... howerver that doesn't necessarily mean they will be 30 days from now; suppose I want already to publish something that is going to happen in 60 days or next year... How to set a costraint for the query to display only the posts occurring in the next 30 days or any set amounts of days/period?

1 Answer 1

1

I recently did exactly the same, you'll have to use custom query:

                 $date = time()-86400; /* today */
                 $querystr = "
                    SELECT $wpdb->posts.* 
                    FROM $wpdb->posts, $wpdb->postmeta
                    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
                    AND $wpdb->posts.post_status = 'publish' 
                    AND $wpdb->postmeta.meta_key = 'event_date' 
                    AND $wpdb->postmeta.meta_value > " . $date . " 
                    ORDER BY $wpdb->postmeta.meta_value ASC
                 ";


                 $pageposts = $wpdb->get_results($querystr, OBJECT);
                 foreach ($pageposts as $post):
                 /* etcetera */

Hope it helps...

Code for changing dd/mm/yyyy to unix timestamp:

$date = explode ('/',$date);
$date = $date[1].'/'.$date[0].'/'.$date[2]; // dd/mm/yyyy to mm/dd/yyyy
$date  = strtotime($date); /* UNIX TIMESTAMP */
9
  • oh hi, I edited my post while you replied... I will try your code; but how to compare today and 30 days from now? thanks!
    – unfulvio
    Commented Dec 2, 2011 at 17:43
  • shouldn't be to hard i guess, just add a second date variable (eg. $date_end = time()+2629743) - and add an extra AND line to the query (not tested, so not 100% sure..)
    – ptriek
    Commented Dec 2, 2011 at 17:50
  • oh yeah, just realized that, thank you :) will try soon and give some feedback
    – unfulvio
    Commented Dec 2, 2011 at 18:08
  • actually I just realized my meta is stored as dd-mm-yyyy ('event_date') but to compare it I should convert it to unix timestamp inside the query or make two queries?
    – unfulvio
    Commented Dec 2, 2011 at 18:10
  • can't you change it to unix timestamp before storage? that's exactly what i did - i'll add the code to my answer..
    – ptriek
    Commented Dec 2, 2011 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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