Order by custom field date?

I’ve created a category “Expire Soon” and i want to insert and sort the posts by custom field “expiration_date”.

I use this code but the posts are sorted by custom field “expiration_date” but not by the date. The posts are sorted by the numbers in the date. Such as:

Read More

31/12/2012
28/01/2013
15/01/2013
04/12/2012

and i want to be like:

04/12/2012
31/12/2012
15/01/2013
28/01/2013

The code i use to insert the custom fields (creating them with Advanced Custom Fields) is:

<?php
                                            if( get_field( "dwra_diagwnismou" ) ): ?>
                                                    <p><b>Δώρα διαγωνισμού:</b>   <?php the_field( "dwra_diagwnismou" ); ?></p>
                                            <?php endif;

                                            if( get_field( "kathgoria-diagwnismou" ) ): ?>
                                                    <p><b>Κατηγορία διαγωνισμού:</b>   <?php the_field( "kathgoria-diagwnismou" ); ?></p>
                                            <?php endif;

                    if( get_field( "diorganwths_diagwnismou" ) ): ?>
                                                    <p><b>Διοργανωτής διαγωνισμού:</b>   <?php the_field( "diorganwths_diagwnismou" ); ?></p>
                                            <?php endif;

                    if( get_field( "hmeromhnia_lhkshs" ) ): ?>
                                                    <p><b>Ημερομηνία λήξης:</b>   <?php the_field( "hmeromhnia_lhkshs" ); ?></p>
                                            <?php endif;

                    if( get_field( "wra_lhkshs" ) ): ?>
                                                    <p><b>Ώρα λήξης:</b>   <?php the_field( "wra_lhkshs" ); ?></p>
                                            <?php endif;

                    if( get_field( "periorismos_perioxhs" ) ): ?>
                                                    <p><b>Περιορισμός περιοχής:</b>   <?php the_field( "periorismos_perioxhs" ); ?></p>
                                            <?php endif;

                    if( get_field( "apaitei_logariasmo_facebook" ) ): ?>
                                                    <p><b>Απαιτεί λογαριασμό Facebook:</b>   <?php the_field( "apaitei_logariasmo_facebook" ); ?></p>
                                            <?php endif;

                    if( get_field( "syxnothta_diagwnismou" ) ): ?>
                                                    <p><b>Συχνότητα διαγωνισμού:</b>   <?php the_field( "syxnothta_diagwnismou" ); ?></p>
                                            <?php endif;

                    if( get_field( "link_diagwnismou" ) ): ?>
                                                    <p><b>Link διαγωνισμού:</b>   <a href="<?php the_field( "link_diagwnismou" ); ?>"><?php the_field( "link_diagwnismou" ); ?></a></p>
                                            <?php endif;

                    if( get_field( "oroi_diagwnismou" ) ): ?>
                                                    <p><b>Όροι διαγωνισμού:</b>   <?php the_field( "oroi_diagwnismou" ); ?></p>
                                            <?php endif;

                    if( get_field( "plhrofories_diagwnismou" ) ): ?>
                                                    <p><b>Πληροφορίες διαγωνισμού:</b>   <?php the_field( "plhrofories_diagwnismou" ); ?></p>
                                            <?php endif;

                                         ?>

I use it in the loop.php

If anyone can help me sort by date i would be grateful!

@s_ha_dum
This is the code i use in the functions.php to insert the data from the submitted form to the custom fields meta-box in the post edit:

/* ------------------------------------------------
   QuForm Plugin - Form to Post
------------------------------------------------ */

add_action('iphorm_post_process_1', 'mytheme_create_wp_post', 10, 1);

function mytheme_create_wp_post($form)
{
    $title = $form->getValue('iphorm_1_1');
    $content .= 'Δώρα διαγωνισμού: ' . $form->getValueHtml('iphorm_1_30') . '
'; $content .= 'Κατηγορία δώρων: ' . $form->getValueHtml('iphorm_1_39') . '
'; $content .= 'Ημερομηνία λήξης: ' . $form->getValueHtml('iphorm_1_8') . '
'; $content .= 'Ώρα λήξης: ' . $form->getValueHtml('iphorm_1_9') . '
'; $content .= 'Διοργανωτής: ' . $form->getValueHtml('iphorm_1_36') . '
'; $content .= 'Περιορισμός (περιοχή): ' . $form->getValueHtml('iphorm_1_15') . '
'; $content .= 'Απαιτεί λογαριασμό Facebook: ' . $form->getValueHtml('iphorm_1_26') . '
'; $content .= 'Όροι διαγωνισμού: getValueHtml('iphorm_1_32') . '">' . $form->getValueHtml('iphorm_1_32') . '
'; $content .= 'Πληροφορίες διαγωνισμού: ' . $form->getValueHtml('iphorm_1_35') . '
'; $content .= 'Link διαγωνισμού: getValueHtml('iphorm_1_11') . '">' . $form->getValueHtml('iphorm_1_11') . '
'; $post = array( 'post_title' => $title, 'post_content' => $content ); // Insert the post $post_id = wp_insert_post($post); // Insert the Custom Fields add_post_meta($post_id, 'dwra_diagwnismou', $form->getValue('iphorm_1_30')); add_post_meta($post_id, 'kathgoria_diagwnismou', $form->getValue('iphorm_1_39')); add_post_meta($post_id, 'diorganwths_diagwnismou', $form->getValue('iphorm_1_36')); add_post_meta($post_id, 'hmeromhnia_lhkshs', $form->getValue('iphorm_1_8')); add_post_meta($post_id, 'wra_lhkshs', $form->getValue('iphorm_1_9')); add_post_meta($post_id, 'periorismos_perioxhs', $form->getValue('iphorm_1_15')); add_post_meta($post_id, 'syxnothta_diagwnismou', $form->getValue('iphorm_1_31')); add_post_meta($post_id, 'apaitei_logariasmo_facebook', $form->getValue('iphorm_1_26')); add_post_meta($post_id, 'link_diagwnismou', $form->getValue('iphorm_1_11')); add_post_meta($post_id, 'oroi_diagwnismou', $form->getValue('iphorm_1_32')); add_post_meta($post_id, 'plhrofories_diagwnismou', $form->getValue('iphorm_1_35'));

Related posts

Leave a Reply

1 comment

  1. There are similar posts on this site about the same topic, if you look around. But here is your answer: To make this work you need to change your date format.

    You can sort alphabetically, or numerically, not calender-merically. The only human date format that orders correctly is YYYY/MM/DD. The separator are optional and shouldn’t matter. Your format is never going to sort the way you want.

    Alternately, you could store your dates in Unix time format and convert them to ‘human’ when you display them, which is what I’d do.

    You could also write your own function to sort things but you will need to query and then sort, which is extra overhead.