Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am adding a new "Custom Post Type" named "Seminar". I am defining three custom fields

  1. Location
  2. StartTime
  3. EndTime

StartTime and EndTime are going to store dates (surprise, surprise). I am wondering if there's any way to tell WordPress to treat these like dates so that I can sort by them and stuff.

I am very new to WP, so forgive me if this is a dumb question.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Hia.

Basically no. But, you can order by meta_value in WP_Query, so you can sort. I am not sure what format you are storing the dates as, but it will need to be something that MySQL can order by, a simple Unix timestamp would be fine, you would do:

$query = new WP_Query( 'post_type=seminar&meta_key=start_time&meta_value=' . time() .  '&meta_compare=>' );

Would get all the seminars with a start date after "now"

share|improve this answer
add comment

The custom field value is stored as is - and I think you can use Joe's answer to get the posts, however to "sort" using start time you might need to use something like this:

$posts = query_posts($query_string. '&orderby=meta_value&meta_key=start_time&order=DESC' ); 

There is a plug-in called more fields, that I think will let you do this without writing code. You can add a new input box only to your custom post type, then select date under advanced options.

share|improve this answer
    
Ugh... I wish I had known about "More Fields" plugin at the beginning of the day. There goes 8 hours. –  jessegavin Aug 19 '10 at 20:52
1  
For the query example - you would probably want order=ASC to get the closest event first (as they are in the future) –  Joe Hoyle Aug 19 '10 at 21:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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