2

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:

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'));
1
  • 1
    Please care about the formatting of your questions (especially your code).
    – kaiser
    Jan 2, 2013 at 22:50

1 Answer 1

4

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.

10
  • What you said is to change the date format to YYYY/MM/DD and then create a function that change this date format to another ,for example: d/m/Y? If yes, how can i do this? I want the custom fields date to be displayed in the post in the d/m/Y format but i want them sorted. Is the code i use correct or i have to change things?
    – Charitos
    Jan 2, 2013 at 21:51
  • Edit your question and add the code that creates and inserts your "expiration_date" meta_value.
    – s_ha_dum
    Jan 2, 2013 at 21:54
  • The "hmeromhnia_lhkshs" I use in the code is the "expiration_date" but it is in Greek words.
    – Charitos
    Jan 2, 2013 at 22:09
  • How do you insert the fields?
    – s_ha_dum
    Jan 2, 2013 at 22:10
  • I editted the question and added the code that i insert the custom fields into the loop for singe posts!
    – Charitos
    Jan 2, 2013 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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