Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Sort Posts by Post Expiration Date in WordPress

Last updated on by
Elegant Themes
How to Sort Posts by Post Expiration Date in WordPress

In the past, we have shared how to expire posts in WordPress using the Post Expirator Plugin. Well, when creating an event listing site, we found this plugin to be super helpful. We can easily delete the event listings that are expired. Secondly, thanks to this plugin, it is really easy to sort posts by post expiration date as well. In this article, we will show you how to sort posts by post expiration date in WordPress.

Code Updated to reflect the 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 that holds like one event per month, so we simply created a loop to put in their events page. You can use the code in your sidebar, or anywhere else you want.

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

The trick is to utilize the meta_key and then user oderby meta_value. Putting it in Ascending Order shows the event that is coming soon (expiring soonest). Example of our events listing:

Sort Posts by Post Expiration (Event Example)

Note in order for this to work, you MUST use the Post Expirator Plugin.

If you have any questions, feel free to ask in the comments below.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • JP

    Posts like this are why I love the internet (and blogs like yours). This is exactly what I was looking for and couldn’t have been laid out more simply. Thanks!

  • Dan

    Just what I was looking for – thanks!

  • Tajim

    Just wanted to say that in current version of this plugin the custom field name has changed from
    expiration-date to _expiration-date.

    The custom field is hidden. So please update the code above.

    Regards
    Tajim

  • AMBA JUNIOR

    Thanks for this. Was wondering how you added the date images to the posts

    • http://www.wpbeginner.com Editorial Staff

      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.

      • Amba Junior

        Thanks for the feedback. Would it be OK to get a sample code from you on this? Thanks in advance.

        • http://www.wpbeginner.com Editorial Staff

          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?

    • http://www.wpbeginner.com Editorial Staff

      You would need to use the get_post_meta function and call the meta key “expiration-date”

      Example would be something like this:

      $expdate = get_post_meta($post->ID, 'expiration-date', true);
      echo $expdate;
      
      • 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″

        Thanks,

  • Kitsts

    So for a total beginner, where would I put this code for it to sort by expiration?

    • http://www.wpbeginner.com Editorial Staff

      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.

      • 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.

        • http://www.wpbeginner.com Editorial Staff

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

          The reason why we can’t answer KITST’ question is because it is impossible to answer with the information provided. The code shared in the article is a standard WordPress loop. Where you paste it in your theme will vary based on your needs and also the type of theme you are using.

          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…

    • http://www.wpbeginner.com Editorial Staff

      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.

      • 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)….

        • http://www.wpbeginner.com Editorial Staff

          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.

  • http://youronline.biz/ 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.