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'));
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.