WordPress Auto Post Expire Plugin

13th May 2011
By

The free WordPress Auto Post Expire from Electric Studio plugin allows you to set a expiry date on a post. At the set date, the post will change status into a draft and no longer display on your site.

The administration panel for this plugin that allows you to choose which post type (including custom post types) you would like to the plugin to be available on.

The Auto Post Expire plugin is great when you have posts about events, offers or other time sensitive information that needs to be automatically removed from your website.

If you want to show a date on the front-end of your site, you can use the shortcode of [expirydate format="m-d-Y"] (The format part is optional).
Developer Note: Instead of using a shortcode you can use function `esape_get_expiry_date($dateFormat = “d-M-Y”);`.

You can download the plugin at the official WordPress site.

Update: Good news, everyone!

So 1.4 of the plugin was reported broken by a number of people, so I went ahead and rewrote the whole thing to make it work nice with 3.4.2. I removed code that was using direct SQL through the database global object (if this doesn’t mean anything to you, worry not :) ). Here’s a short run-down of what happened to it, how to use, possible pitfalls, etc.

Stuff changed

jQuery and jQuery UI

Version 1.4 pulled in external scripts. This was changed in 1.5, so it is using the scripts shipped with WordPress itself. Makes code leaner, and less likely to break, plus, as you update WordPress, the scripts will get better as well.

Time

Not just date. Beforehand you set a day to expire posts. That meant that one minute after 11:59pm of the previous day, the post would disappear. What if you wanted it to expire midday? Also this data is now stored as a timestamp, which basically every known programming language understands and can produce a dateformat of your choosing. More on this later.

FAQ & Howtos

Is it going to save my preferences from 1.4?

Yes, all the options / expiry dates should carry over to 1.5 no problem.

Can I display the expiry date in the post, or in the code?

Yes. The shortcode [expirydate] is there, or you can use es_ape_exp_date() function for that in your code.

What about customizing the dateformat?

The full reference for both the shortcode and the function is below:

[expirydate format="d-M-Y"]

The default is “d-M-Y”. You can replace that with anything that is a valid php date() syntax. So if you only want the year, you would use [expirydate format="Y"], if you want to add the time as well, use [expirydate format="d-M-Y G:i"]

es_ape_exp_date( $format, $echo );

$format (string) is the same format used by the shortcode. Default is “d-M-Y”.

$echo (bool) is either true or false. True echoes the date, false returns it.

I want to get the timestamp itself, but es_ape_exp_date() will only give me a formatted date!

Fear not, for that isn’t that hard. Use this (assuming you are within The Loop):

$timestamp = get_post_meta( get_the_ID(), "es_ape_expiry", true );

You can then manipulate it however you like. Substract a week, and show an additional message if it’s going to expire soon ( “Hurry, offer ends soon!” ), or something like that. This is in seconds, so a week would be 7*24*60*60 = 604800 seconds.

Can I order the posts by expiry date (soonest to expire comes first, for example)?

Yes. Use this:

$args = array(
    'post_status' => 'publish',
    'meta_key' => 'es_ape_expiry',
    'order' => 'ASC',
    'orderby' => 'meta_key_num'
);

$ordered_posts = new WP_Query( $args );

if( $ordered_posts->have_posts() ) {
    while( $ordered_posts->have_posts() ) {
        $ordered_posts->the_post();
        # your code here ....
    }
} else {
    echo "No posts found";
}

Possible pitfalls

As I inherited the previous developer’s code, and decided the best course of action is to understand what it does, and rewrite it using the WordPress way ( “there’s a function for that” ), there might be instances where previously solved problems would crop up again.

For support, check out the plugin’s wordpress support page.