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.

The query below works as expected, showing cpt event ordered by meta_value, what I haven't figured out yet is how to remove posts for past events or events which have started. The start date for events is currently stored as, 'yy m d' and the date is chosen using jQuery datepicker.

<?php
global $post;
$args = array(
        'post_type' => 'events',
        'numberposts' => 5,
        'meta_key' => 'sp_startDate',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    );
$supplierTrainingEvents = get_posts( $args );
foreach( $supplierTrainingEvents as $post ) :   setup_postdata($post);
?>

I've tried this, and other variations, with no luck.

$args = array(
'post_type' => 'events',
'numberposts' => 5,
'meta_key' => 'sp_startDate',
'meta_value' => date("MM d yy"),
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
 );

Thank you for taking a look.

share|improve this question
    
you're going to have issues with that date format. if you look at it from a numerical perspective it should be Y m d, otherwise 03-07-2012 < 03-08-2011 –  Milo Mar 7 '12 at 16:40
    
Can you store your date as a Unix timestamp? –  m0r7if3r Mar 7 '12 at 16:56
    
@Milo good observation, but that must not be the only issue as it doesn't alter the result, however it did make me wonder if this has something to do with it, the date is being selected with a jQuery date picker, which uses a different scheme for formatting the date. –  Chuck Mar 7 '12 at 16:59
    
@m0r7if3r I had tried that using @ for the jQuery datepicker, however in the admin it displayed as an unrecognizable date, which isn't an option for the end user of this site. –  Chuck Mar 7 '12 at 17:04
    
You can use strtotime() to convert from any date format to a unix time stamp. –  m0r7if3r Mar 7 '12 at 17:10

1 Answer 1

up vote 3 down vote accepted

The date format is incorrect. I'm not sure how you have stored the date, but the format you've used in the query, date("MM d yy") outputs 'MarMar 07 1212'.

It shouldn't really matter that the input is being done by jQuery datepicker: The input should be in a pre-specified format - so you can interpret input it as a date (or preferably DateTime) object and then format that date in this format: 2012-03-07, which in php 'format' is 'Y-m-d'.

Alternatively you can store it as a time-stamp, if you are not bothered about 2038.

Finally be weary of using php date functions. WordPress sets the 'internal' timezone to UTC - its better to use WordPress functions which will account for the timezone of the blog.

share|improve this answer
    
The problem was that I was assuming jQuery date picker and php were using the same scheme for dates, so I just copy pasted from one to the other. So now I use 'yy m d' for the datepicker and 'Y n j' in the query. –  Chuck Mar 7 '12 at 17:27
1  
You'll probably find you need to use 'Y-m-d'. Most date functions would be able to interpret such a string, and also without leading zeros the sorting might be out. Consider: '2012 1 3', '2012 11 31' and '2012 2 3' –  Stephen Harris Mar 7 '12 at 17:47
    
Thanks, I'm sure that last little tip saved me some head scratching down the road. –  Chuck Mar 7 '12 at 18:42

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.