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 created a custom "events" post type for a client that basically "hides" the post once it has expired (ie. the event already happened). The events are displayed in the order of the start date rather than the order that they are posted in. This is, ultimately, all working fine. However, now that we're closing in on the end of the year it appears that my comparison operator doesn't allow any posts from next year to be shown and I'm not totally sure how to change the operator to make this work properly.

    $args = array(
                    'post_type'         => 'event',
                    'post_status'       => 'publish',
                    'posts_per_page'    => $num,
                    'paged'             => $paged,
                    'meta_key'          => 'startDate',
                    'meta_query'        => array(
                                                array( 
                                                    'key'     => 'startDate',
                                                    'value'   => date("m/d/Y"),
                                                    'compare' => '>=',
                                                    'type'    => 'NUMERIC'
                                                )
                                            ),
                    'orderby'           => 'meta_value',
                    'order'             => 'ASC'
        );
    $e_query = new WP_Query($args);

    // get the current date to compare to post's dates
    $currentDate = date("m/d/Y");

    if($e_query->have_posts()){
        // while we have posts
        while($e_query->have_posts()) : $e_query->the_post();

            // get the post's start date to compare to the current date
            $startDate_compare = date('m/d/Y', strtotime(get_post_meta(get_the_ID(), "startDate", true)));

            if ($startDate_compare >= $currentDate) {
                // this is where all my code to output the post's data lives and which is ultimately working fine except for the fact that no posts that are dated to start in 2012 will show up here


            }


        endwhile;
    } else{
        $e_content .= '<p>Sorry, there is currently no events scheduled at this time.</p>';
    }

It appears to be something to do with my meta_query because I just attempted outputting the post_titles before I make the

if($startDate_compare >= $currentDate){ 

comparison and only the posts that happen in 2011 show up.

Thank you for any help you may have for me.

UPDATE IN RESPONSE TO user9347's COMMENT

When I changed the "type" of the meta_query to "date" which, ultimately, should've worked, the custom post type is returning that there are no posts found which to me says that there is an issue with how the value is being saved potentially for the "startDate" key? I would think that the 'date("m/d/Y")' in the meta_query's "value" would fix this... but then I'm not a super knowledgeable php developer... more of a hacker than anything.

This is the code for my custom meta field where my client picks a startDate with the jquery datepicker. The jquery datepicker returns the date value like this: "02/18/2012".

    <tr valign="top">
        <th scope="row" style="width: 200px; text-align: left; vertical-align:top;">Start Date</th>
        <td><input type="text" name="startDate" id="startDate" class="datepicker" style="width: 200px;" value='<?php echo $startDate; ?>' /></td>
    </tr>

then in a function attached to the "save_post" action, it is saved as such:

update_post_meta($post->ID, 'startDate', $_POST['startDate']);

It seems to me that I need to save this 'startDate' value as a date object in order to make the meta_query work the way it should... would this just look something like this?

$startDateToTime = date('m/d/Y', strtotime($_POST['startDate']);
update_post_meta($post->ID, 'startDate', $startDateToTime);

*This last part didn't seem to work when I created a new post, saved it, and changed the meta_query 'type' to 'DATE', so I'm still a little lost here.*

share|improve this question

2 Answers 2

Use type 'DATE'. You are using NUMERIC. Your date value is not numeric.

share|improve this answer
    
Unfortunately, when I use "DATE" my "if($e_query->have_posts())" evaluates to false and I am told there are no events scheduled at this time. Would it happen to have something to do with how my startDate field is being saved? I'm using a jquery datepicker that pushes it into an input text field in the format "02/18/2012". I am going to update the question with further code samples. –  David Odin Kether Dec 9 '11 at 20:59
up vote 0 down vote accepted

I ended up solving this by doing essentially one thing in a couple places. Wordpress doesn't let you specify a particular type for your meta data and stores everything as strings. As such, I added an extra meta data field that I used as the actual timestamp (using strtotime to convert it to a unix numeric timestamp) that gets created when the post is saved from the $startDate value.

$startDateTimeStamp = strtotime($_POST['startDate']);

From there I made my original meta_query order the posts by this new meta data value instead:

$args = array(
                    'post_type'         => 'event',
                    'post_status'       => 'publish',
                    'posts_per_page'    => $num,
                    'paged'             => $paged,
                    'meta_key'          => 'startDateTimeStamp',
                    'orderby'           => 'meta_value_num',
                    'meta_query'        => array(
                                                array( 
                                                    'key'     => strtotime('startDateTimeStamp'),
                                                    'value'   => strtotime(date("m/d/Y")),
                                                    'compare' => '>=',
                                                    'type'    => 'NUMERIC'
                                                )
                                            ),
                    'order'             => 'ASC'
        );
    $e_query = new WP_Query($args);

*Please note: * This query wouldn't work until I made sure that I ran the meta query's key/value through the strtotime function and changed the orderby to 'meta_value_num'.

share|improve this answer

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.