1

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.

5
  • 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, 2012 at 16:40
  • Can you store your date as a Unix timestamp?
    – mor7ifer
    Mar 7, 2012 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, 2012 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, 2012 at 17:04
  • You can use strtotime() to convert from any date format to a unix time stamp.
    – mor7ifer
    Mar 7, 2012 at 17:10

1 Answer 1

4

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.

3
  • 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, 2012 at 17:27
  • 2
    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' Mar 7, 2012 at 17:47
  • Thanks, I'm sure that last little tip saved me some head scratching down the road.
    – Chuck
    Mar 7, 2012 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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