web-dev-qa-db-fra.com

Commander par date de champ personnalisé?

J'ai créé une catégorie "Expire bientôt" et je souhaite insérer et trier les articles par champ personnalisé "expiration_date".

J'utilise ce code mais les articles sont triés par champ personnalisé "expiration_date" mais pas par date. Les messages sont triés par les nombres dans la date. Tel que:

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

et je veux être comme:

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

Le code que j'utilise pour insérer les champs personnalisés (en les créant avec des champs personnalisés avancés) est le suivant:

<?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;

                                         ?>

Je l'utilise dans le loop.php

Si quelqu'un peut m'aider à trier par date, je vous en serais reconnaissant!

@s_ha_dum C'est le code que j'utilise dans le functions.php pour insérer les données du formulaire soumis dans la méta-boîte de champs personnalisés dans la post-édition:

/ * ---------------------------------------------------- 
 QuForm Plugin - Formulaire pour publier 
 ---------------------------------- -------------- */
 
 add_action ('iphorm_post_process_1', 'mytheme_create_wp_post', 10, 1); 
 
 fonction 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. =' Lien διαγωνισμού: getValueHtml ('iphorm_1_11'). '">'. $ form-> getValueHtml ('iphorm_1_11'). '
'; $ post = array ( ' post_title '=> $ title, ' post_content '=> $ content ); // Insérer le message $ Post_id = wp_insert_post ($ post); // Insérer les champs personnalisés Add_post_meta ($ post_id, 'dwra_diagwnismou', $ form-> getValue ('iphorm_1_30')); Add_post_meta ($ post_id, 'kathgoria_diagwnismou', $ form-> getVal 'iphorm_1_39')); add_post_meta ($ post_id, 'diorganwths_diagwnismou', $ form-> getValue ('iphorm_1_36')); add_post_meta ($ post_id, 'hmeromhnia_lhhs) getValue ('iphorm_1_8')); add_post_meta ($ post_id, 'wra_lhkshs', $ form-> getValue ('iphorm_1_9')); add_post_meta ($ post_id, 'periorismos_perioxhs', -> getValue ('iphorm_1_15')); add_post_meta ($ post_id, 'syxnothta_diagwnismou', $ form-> getValue ('iphorm_1_31')); add_post_meta ($ post_id.) $ form-> getValue ('iphorm_1_2 6 '); Add_post_meta ($ post_id,' link_diagwnismou ', $ form-> getValue (' iphorm_1_11 ')); Add_post_meta ($ post_id,' ouoi_diagwnismou ', $ form-> getValue ('iphorm_1_32')); add_post_meta ($ post_id, 'plhrofories_diagwnismou', $ form-> getValue ('iphorm_1_35'));
2
Charitos

Si vous regardez autour de vous, il y a des publications similaires sur ce site sur le même sujet. Mais voici votre réponse: pour que cela fonctionne, vous devez changer le format de votre date.

Vous pouvez trier par ordre alphabétique ou numérique, et non par calendrier. Le seul format de date humain qui commande correctement est YYYY/MM/DD. Les séparateurs sont facultatifs et ne devraient pas avoir d’importance. Votre format ne va jamais trier comme vous le souhaitez.

Alternativement, vous pouvez stocker vos dates au format heure Unix et les convertir en "humain" lorsque vous les affichez, ce que je ferais.

Vous pouvez également écrire votre propre fonction pour trier les choses, mais vous devrez interroger puis trier, ce qui représente une surcharge supplémentaire.

4
s_ha_dum