Par le passé, nous avons partagé comment faire expirer des articles dans WordPress à l'aide du Plugin Post Expirator. Eh bien, en créant un site de liste d'événements, nous avons trouvé ce plugin très utile. Nous pouvons facilement supprimer les listes d'événements expirées. Deuxièmement, grâce à ce plugin, il est également très facile de trier les articles par date d'expiration. Dans cet article, nous vous montrerons comment trier les articles par date d'expiration dans WordPress.
Code mis à jour pour refléter les changements dans le plugin où ils ont changé le nom du champ personnalisé. Merci Tajim de nous avoir informés dans les commentaires.
Dans notre projet particulier, nous avions des Événements en tant que Type de Publication Personnalisé. Il s'agit maintenant d'une organisation à but non lucratif qui organise environ un événement par mois, nous avons donc simplement créé une boucle à mettre sur leur page d'événements. Vous pouvez utiliser le code dans votre barre latérale, ou n'importe où ailleurs que vous le souhaitez.
<?php $args = array( 'post_type' => 'event', 'posts_per_page' => 15, 'order' => 'ASC', 'meta_key' => '_expiration-date', 'orderby' => 'meta_value' ); $eventloop = new WP_Query( $args ); if ( $eventloop->have_posts() ) : while ( $eventloop->have_posts() ) : $eventloop->the_post(); //All the Loop Content Goes Here endwhile; endif; ?>
L'astuce consiste à utiliser meta_key puis user oderby meta_value. Le mettre en ordre croissant montre l'événement à venir (qui expire bientôt). Exemple de notre liste d'événements :

Notez que pour que cela fonctionne, vous DEVEZ utiliser le Plugin Post Expirator.
Si vous avez des questions, n'hésitez pas à les poser dans les commentaires ci-dessous.

Francis Waller
Bonjour, j'essaie de faire fonctionner ceci sur mes produits WooCommerce qui doivent être manipulés pour être des cours hors site qui expirent à une date précise. J'ai vraiment besoin de les trier par date d'expiration, je ne suis pas sûr de ce que je fais de mal. Merci
PAO
Est-il possible d'afficher les publications qui se terminent demain ou la semaine prochaine, le mois prochain, etc.
Je veux créer un site qui affiche toutes les publications qui se terminent demain. et une qui affiche toutes les publications qui se terminent la semaine prochaine.
Des suggestions ?
James Parkin
J'ai essayé de faire fonctionner ce code avec un thème enfant Genesis Pro. J'ai créé plusieurs articles avec des dates futures. J'ai ajouté ce code aux barres latérales via des shortcodes et j'ai essayé dans le fichier functions.php. Je n'utilise aucun champ personnalisé ni n'ajoute d'informations.
Toutes idées seraient appréciées
James
J'ai essayé de faire fonctionner ceci en vain. Je l'ai posté dans la barre latérale, functions.php et comme shortcodes. Toute aide serait appréciée.
James
Je n'ai pas réussi à faire fonctionner ceci avec un thème enfant Genesis Pro. Je l'ai ajouté aux dernières lignes du fichier functions.php. J'ai également ajouté des valeurs méta. Je ne suis pas sûr pourquoi il ne publie pas dans l'ordre ASC par date d'expiration.
merci d'avance
JP
Les articles comme celui-ci expliquent pourquoi j'aime internet (et les blogs comme le vôtre). C'est exactement ce que je cherchais et cela n'aurait pas pu être présenté plus simplement. Merci !
Dan
Exactement ce que je cherchais – merci !
Tajim
Je voulais juste dire que dans la version actuelle de ce plugin, le nom du champ personnalisé est passé de
expiration-date à _expiration-date.
Le champ personnalisé est masqué. Veuillez donc mettre à jour le code ci-dessus.
Regards
Tajim
AMBA JUNIOR
Thanks for this. Was wondering how you added the date images to the posts
Personnel éditorial
We pulled the meta information that this plugin stores in the post custom fields. Then styled it. That was a background CSS image and text overlay.
Admin
Amba Junior
Thanks for the feedback. Would it be OK to get a sample code from you on this? Thanks in advance.
Personnel éditorial
Unfortunately, we don’t offer that as part of our free service. You are more than welcome to hire us to write a custom snippet for you.
Steve Combes
Thanks for the great post.
If I wanted to echo the post expiration date in the loop what code might I use?
Personnel éditorial
You would need to use the get_post_meta function and call the meta key “expiration-date”
Example would be something like this:
1-click Use in WordPress
Admin
Adam
I changed it to ‘_expiration-date’ and I’m getting the data, but how would you re-sort this into the correct date display? My date is March 1st 2015 and what’s echoed is “1425254340”
Merci,
Kitsts
So for a total beginner, where would I put this code for it to sort by expiration?
Personnel éditorial
This article is not for total beginners. Because you have to have fair understanding of how WordPress themes work in order to make this code work with your specific theme.
Admin
CuriousObserver
If this isn’t for beginners, why did you post it to wpbeginner.com? I, too, am having difficulty inserting this into my blog and would like to see an answer to KITSTS’ question.
Personnel éditorial
Here at WPBeginner, we define beginners into various levels. Beginner blogger aka total beginner (just starting to blog, doesn’t know what is RSS, FTP etc). Beginner theme designer (knows HTML+CSS, but has no idea how to build themes in WordPress). Beginner plugin developer (knows the general concept around programming, but not specific to WordPress).
La raison pour laquelle nous ne pouvons pas répondre à la question de KITST est qu'il est impossible d'y répondre avec les informations fournies. Le code partagé dans l'article est une boucle WordPress standard. L'endroit où vous le collez dans votre thème variera en fonction de vos besoins et du type de thème que vous utilisez.
If you are using a child theme, then most likely that code will end up in your functions.php file, so you can hook it into the function provided by the theme framework.
If you are using another WordPress theme, then the code can go in various different template files (index.php, sidebar.php, archive.php, archive-{posttype}.php, footer.php, header.php, etc). Each theme is organized differently which makes it impossible for us to give you an exact location where to paste this code.
Also note, that the loop that we shared above is from an example work that we created using a custom post type called events. You may not want to use it for a custom post type events. Perhaps you want to use it for a different post type called deals.
This article was intended for beginner theme designers and/or beginner developers who are looking for an easy way to do this.
G J
I was wondering, which plugin is used for displaying the upcoming events in the example above?
Thanks for a lovely article…
Personnel éditorial
That wasn’t a plugin. It was just custom styling a custom post type display using the loop code that we shared in this article.
Admin
G J
Ahhh..ok, is it possible for you to give an example on “All the Loop Content Goes Here” I am updating the webpage of a NGO and kind of liked this way of displaying the events on. As you may have noticed I am a beginner so I would really appreciate your help ;o)….
Personnel éditorial
The loop code essentially is the_title(); php tag to show the title. Another custom field to show the date. The loop is explained pretty well here:
http://codex.wordpress.org/The_Loop
We used styling (div/images/css) to make it look that way. This post is about how to sort posts by expiration date, and the article shows just that. The example used is something that we did for our client, so unfortunately we cannot share the images/styling that they paid for. Send us an email if you are interested in having us do this for you. We have special non-profit rates.
Jacko
This is a cool feature. There are several situations where this is what you need to be doing.
PS
This website has one of my favorite designs. The #IBCT is about finding the best blogger in the world. Your website has made it to the semis.