• Resolved kim crimson

    (@kim-crimson)


    I’m making a site where on one page I need to display two individual loops from different categories. All is working fine except the posts have to be sorted by their custom field value which is a date.

    Here is the loop i’m using:

    <?php
    $project_date =   get_posts('category=8&numberposts=30&order=ASC&orderby=meta_value&meta_key=Dato');
    foreach ($project_date as $post) :
    setup_postdata($post);
    $dato = get_post_meta($post->ID, 'Dato', $single = true);
    $type = get_post_meta($post->ID, 'Licitationstype', $single = true);
     ?>

    But somehow It won’t sort the dates correctly. The site is in Danish and the dates have to be in this format: 25. august 2009 (‘j. F, Y’). I have written them this way in the custom fields, but this is what comes up:

    18. august 2009
    19. august 2009
    2. september 2009
    24. august 2009
    25. august 2009

    I looks like it’s sorting them based on the first number and ignoring the month. So I’m wondering how to tell the loop that the value is a date?

    Any help on this will be so very much appreciated!!!!!!!

Viewing 15 replies - 1 through 15 (of 15 total)
  • Try entering your custom dates in the format: yyyy-mm-dd.

    Thread Starter kim crimson

    (@kim-crimson)

    Thanks, but that’s unfortunately not an option for me. The date has to be displayed this exact way.

    I’ve tried many solutions, but they all turn out the same. I just need some kind of way of telling the loop that the custom field is a date in this format.

    Thread Starter kim crimson

    (@kim-crimson)

    Anyone?

    Sorry to bump.. I’m just hoping this is possible to do..

    The date has to be displayed this exact way.

    Just because the date has to be displayed using format A doesn’t mean it can’t be entered as a meta value using format B.

    Thread Starter kim crimson

    (@kim-crimson)

    Just because the date has to be displayed using format A doesn’t mean it can’t be entered as a meta value using format B.

    okay, thanks! but will the dates still be sorted with format A, even though format b is displayed.

    Can you give me an example of how to do this?

    thanks again!

    If you used ..

    02. september 2009

    instead of..

    2. september 2009

    does it make any difference?..

    The reason that the dates sort that way is because they sort by the first number, in this case the 2 is greater than the first 1 in say this line..

    18. august 2009

    Thread Starter kim crimson

    (@kim-crimson)

    does it make any difference?..

    Not really, then it just puts it first, because the date starts with a 0. And even if that worked it would still sort the months alphabetically..

    Then as Esmi said, use the other date format, then re-arrange the data when you retrieve it.

    Thread Starter kim crimson

    (@kim-crimson)

    Then as Esmi said, use the other date format, then re-arrange the data when you retrieve it.

    Can you give me an excample on how to re-arrange the data when I retrieve it?

    Thanks so much!

    That really depends on the format you type the date in… you’d need to test the sort order before commiting time in reformatting it for display, and i don’t have posts that exist like this to test it directly myself.

    If you used..

    yyyy-mm-dd then you could use explode (can provide an example) to split the data up, then implode (can provide an example) to join it together after resorting it.

    I’m not a huge user of date functions, there’s proberly a date function better suited to the job but it’s really not something i’d know without testing.

    The point i’d make though is, are you happy with using a different format in the field?
    If not, then it’s proberly a waste of time producing an example if you have a distinct requirement to keep the format you originally stated.

    Although (thinking out loud) i don’t see why it wouldn’t be possible to resort the posts using an array sorting method before the foreach loop, that way avoiding any need to change the date format.

    I’ll go make some test posts…

    Esmi said:

    Try entering your custom dates in the format: yyyy-mm-dd.

    Yes. It is the International date format as per ISO 8601.

    S.K

    I think that would be best, from my testing (as bad or good as i am, lol) it would take alot of shuffling around code, just to resort the posts.. using your date format.

    I think ultimately you’d be creating initial work for the server for very little return…

    Where as if you used yyyy-mm-dd when entering the date into the field you could then just reformat the data for display…

    mktime and date functions could be used along with explode to switch the display of the date parts.. although i’m not sure if that’s necessarily required.

    I think it’s a case of…

    $dato = get_post_meta($post->ID, 'Dato', true);
    $test = explode('-',$dato);
    echo date('j. D, Y', mktime(0,0,0,$test[1],$test[2],$test[0]));
    unset($test);

    That’s off the top of my head, but i think something along those lines would do it…

    Thread Starter kim crimson

    (@kim-crimson)

    That’s off the top of my head, but i think something along those lines would do it…

    wow.. That’s working perfectly!! Now they are sorted correctly. The only remaining problem is that the months are now in English. And they have to be Danish.

    I looked around the Codex and found this:

    <?php
    setlocale(LC_ALL, 'da_DK.ISO-8859-15@euro');
    echo strftime('%A %d %B, %Y',strtotime(get_the_time('j. D, Y')));
    ?>

    But I don’t really have a clue about where to put it.. All this date stuff is pretty new to me..

    Thanks so much again!

    Thread Starter kim crimson

    (@kim-crimson)

    Actually I figured it out. I just have to use WP’s date function date_i18n. So the custom field call looks like this:

    <?php
    $dato = get_post_meta($post->ID, 'Dato', true);
    $test = explode('-',$dato);
    echo date_i18n('j. F Y', mktime(0,0,0,$test[1],$test[2],$test[0]));			unset($test);
    ?>

    Then it displays the date in the right language. So problem solved!! Thanks for all your help t31os_!!!

    No problem.. ??

    You can of course rename the word test to something else, just to make for nicer reading code.. ??

    You can also safely unset the data from $test (or whatever you name it)

    unset($test);

    After the date has been echo’ed, if it’s not needed further along..

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘sorting posts by custom date field not recognizing the date format’ is closed to new replies.