In the past, we have shared how to expire posts in WordPress using the post expiration plugin. Well, when creating an event listing site, we find this plugin to be very useful. We can easily remove event listings that have expired. Secondly, thanks to this plugin, it is also very easy to sort the posts by later expiration date. In this article, we will show you how to sort posts by post-post due date in WordPress.
Updated code to reflect changes in the plugin where they changed the name of the custom field. Thanks Tajim for letting us know in the comments.
In our particular project, we had events as a custom post type. Now this is for a non-profit organization that runs as one event per month, so we just created a loop to put on their events page. You can use the code in the sidebar or anywhere else you want.
'evento', 'posts_per_página' => 15, 'orden' => 'ASC', 'meta_key' => '_expiration-date', 'orderby' => 'meta_value'); $ eventloop = new WP_Query ($ args); if ($ eventloop-> have_posts ()): while ($ eventloop-> have_posts ()): $ eventloop-> the_post (); // Todo el contenido de Loop va aquí al final; terminara si; ?>
The trick is to use meta_key and then user oderby meta_value. Putting it in ascending order shows the next event (expiring as soon as possible). Example of our list of events:
Please note that for this to work you MUST use the post-expiration plugin.
If you have any questions, feel free to ask in the comments below.