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 have defined a custom post type that uses a meta field to store dates.

In my functions.php I have the following code which saves the start date. The format of the date that is sent in the startdate form parameter is 2010/12/01 9:00 AM

add_action('save_post', 'save_details');

function save_details(){
    global $post;
    update_post_meta($post->ID, "startdate", $_POST["startdate"]);
}

I am trying to output a list of all the events with a startdate later than the current time.

query_posts(array(
    'post_type' => array('seminar'),
    'meta_key=startdate',
    'meta_value='.date("Y/m/d h:i A"),
    'meta_compare=>'
));

This lists out all the events regardless of their start date. What am I doing wrong?

SOLVED:

For some reason after I changed my code to the following, it just works.

$args = array(
    'post_type' => array('seminar'),
    'showposts' => 3,
    'meta_key' => 'startdate',
    'meta_value' => date("Y/m/d h:i A"),
    'meta_compare' => '>',
    'orderby' => 'meta_value',
    'order' => 'ASC'
);

$seminars = get_posts($args);

I am going to select the answer from @Rarst as the accepted one, because I think I actually had multiple issues going on here, but his addressed the issue related to the title best.

@sorich87 had a good point about how I should have been storing the dates as timestamps, but in the end, @Rarst was correct that if I specified a format argument to the date() function which matches the format I used when storing the data into the database, then I should be able to compare the data.

Thanks for all the help.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You store date as formatted string, but time() function you take for comparison returns numeric timestamp. So you are trying to make comparison between too completely different formats and it's unlikely WP is smart enough to get that.

share|improve this answer
    
Is there a way for me to store the date as a Date? I thought the only way to store custom meta data was as a string. –  jessegavin Sep 29 '10 at 18:19
    
Well, there is no rule that you can't save timestamp in a string. :) I'd try firt to replace time() with date() of same format as your meta field. I think it should work that way. –  Rarst Sep 29 '10 at 18:26
    
I tried swapping out to use date("Y/m/d h:i A"). That didn't seem to make any difference though I think it should have. –  jessegavin Sep 29 '10 at 19:24
    
Your answer solved the main issue I was having. Thanks –  jessegavin Sep 29 '10 at 20:53

I would convert the date to a timestamp before storing it:

add_action('save_post', 'save_details');

function save_details(){
    global $post;
    update_post_meta($post->ID, "startdate", strtotime($_POST["startdate"]));
}

And use the current timestamp to do the comparison:

query_posts(array(
    'post_type' => array('seminar'),
    'meta_key=startdate',
    'meta_value='.time(),
    'meta_compare=>'
));
share|improve this answer
    
Sadly, that's not enough. Alpha sorted numericals sort as 1,11,2 rather than 1,2,11. :-) What's really needed is an extra field imo. wordpress.stackexchange.com/questions/4852/… –  Denis de Bernardy Dec 6 '10 at 19:00
    
Yes, you are right. –  sorich87 Dec 7 '10 at 0:28

Perhaps this would do the trick?

query_posts(array(
    'post_type' => array('seminar'),
    'meta_key' => 'startdate',
    'orderby'=> 'meta_value',
    'order' => 'ASC'
));
share|improve this answer
    
Your code modifies order, while original question asks about applying filter. They use same query arguments, but do different things. –  Rarst Sep 29 '10 at 20:14
    
Oops! Totally mis-read the question! Sorry! –  Emrikol Sep 29 '10 at 20:19

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.