2

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.

4 Answers 4

2

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.

4
  • 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, 2010 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, 2010 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, 2010 at 19:24
  • Your answer solved the main issue I was having. Thanks
    – jessegavin
    Sep 29, 2010 at 20:53
3

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=>'
));
2
1

Perhaps this would do the trick?

query_posts(array(
    'post_type' => array('seminar'),
    'meta_key' => 'startdate',
    'orderby'=> 'meta_value',
    'order' => 'ASC'
));
2
  • Your code modifies order, while original question asks about applying filter. They use same query arguments, but do different things.
    – Rarst
    Sep 29, 2010 at 20:14
  • Oops! Totally mis-read the question! Sorry!
    – Emrikol
    Sep 29, 2010 at 20:19
0
  1. Save value in database like :

    'enddate'   =>  date('Y-m-d');
    
  2. Generate meta query like :

A. Condition :

$conArr[0]  =    array
(
'key'=> 'enddate',
'value' => date('Y-m-d'),
'compare' => '>=',
);

B. Query:

$args = array(
    'meta_query'=>$conArr ,
    'post_type'=>'tournament',
    'orderby' => 'meta_value_num', 
)

Important 'orderby' => 'meta_value_num', and date format are important.

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.