How to Set WordPress Posts to Automatically Expire After a Certain Number of Days

UPDATED May 6, 2006: This hasn’t been tested in WordPress 2.0. According to this WordPress forum thread, this plugin works in 2.0, so give it a try!

I’ve been helping a friend convert her website to a WordPress CMS and ran into a problem.

FRIEND: With my old software, I could type in a date on any specific post and on that date it would be taken off the website. It was really easy. Can I do that with this… WordPress?

ME: Sure! WordPress can do anything! If there isn’t a feature built-in, somebody’s always designed a plugin for it.

FRIEND: Great. Can we do it now?

ME: Er… let me get back to you on this.

In the frantic searching that ensued, I found Ashwin Bihari’s plugin: Auto Delete Posts.

This is a great little plugin. It works by taking the current date and determining which posts are older than 14 days (14 days is the default, but you can set another number of days) and if any posts are found, they are deleted or moved to another category.

But this wasn’t what my friend wanted. She wanted some posts to automatically expire after 7 days, some after 30 days, and so on.

I did some more research and found this WordPress forum thread: “Post expires after set date or number of days,” which recommends following the WordPress Wiki instructions “Expire Posts.”

Expiring posts turns out to be something that can be done with Custom Fields values. (It says you can do this in the Codex, too, but doesn’t explain how.)

The directions are set up for WordPress version 1.2, but the Wiki says it works for 1.5, too. The index.php page has changed from 1.2 to 1.5, but I figured it out (I thought) and inserted the necessary lines of code. My index.php now looked like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php require('post.php'); ?>
<?php list ($post_expired) = get_post_custom_values('postexpire');
/* only pay attention to the first one if there's a duplicate */ ?>
<?php if ( !$post_expired || current_time('mysql') <$post_expired) :
/* if there's no expiration date or it's not been reached yet, go
ahead with displaying the post */ ?>
<?php endif; /* end not-expired test */ ?>
<?php endwhile; ?>

Then, in the Custom Field area of the Dashboard, I created a “postexpire” Key and a Value of “2005-10-03 01:00:00.”

I didn’t get any error messages, but the post didn’t expire as scheduled, either. I wrote in the WordPress forum for help, but nobody had an answer for me. Almost 2 days later I had to tell my friend I was still working on it.

Anxious not to lose face, I looked back to Ashwin’s plugin. I wrote him and asked if perhaps there was some way I could set his plugin to allow for different expiration values. He kindly wrote back to say no, and then offered to help me with my Custom Field values.

At that moment, this is what I thought of Mr. Ashwin Bihari:
Ashwin

He suggested I rearrange the lines to look like so:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php require('post.php'); ?>

<?php list ($post_expired) = get_post_custom_values('postexpire'); /* only pay attention to the first one if there's a duplicate */ ?>

<?php if ( !$post_expired || current_time('mysql') < $post_expired) : /* if there's no expiration date or it's not been reached yet, go ahead with displaying the post */ ?>

<?php endif; /* end not-expired test */ ?>

<?php endwhile; ?>

It works perfectly. The posts are not deleted. They remain in the database where you can manage them on the “Dashboard –> Manage –> Posts” screen.

You can “un-expire” them, or delete them, or whatever it is you want to do with an expired post. But as long as the date you posted in the Custom Fields Value box matches or is after today’s date, the post will not appear as published.

Ashwin now joins the growing ranks of those selfless WordPress gurus who have saved Bonnie’s bohunney. Thank you, Ashwin!

7 Replies to “How to Set WordPress Posts to Automatically Expire After a Certain Number of Days”

  1. This looks very useful. Where, exactly, should I be placing the suggested code? Thanks

  2. Oy! EWB!

    Remember that I made a mistake trying it out, and Ashwin had to set me to rights. If you’re not sure how to work within your WordPress theme files, then I don’t recommend trying this unless you’re willing to risk making a mistake and messing up your blog.

    Open up your wp-content–> themes–> [your theme]–> index.php file. Before you do anything to index.php, be sure to save a copy of it in a safe place, so that if you mess things up, you can go right back to the “good” copy of index.php.

    Find where “the Loop” begins. If you have the most recent version of WordPress (should be 1.5.2), you want to look for these lines within the index.php file:


    < ?php if (have_posts()) : ?>
    < ?php while (have_posts()) : the_post(); ?>

    …or something similar. “The loop” ends with these lines:


    < ?php endwhile; else: ?>
    < ?php _e('Sorry, no posts matched your criteria.'); ?>
    < ?php endif; ?>

    If you want to try copying and pasting, copy this text over the old Loop:


    < ?php if (have_posts()) : ?>
    < ?php while (have_posts()) : the_post(); ?>
    < ?php list ($post_expired) = get_post_custom_values('postexpire'); /* only pay attention to the first one if there's a duplicate */ ?>
    < ?php if ( !$post_expired || current_time('mysql') < $post_expired) : /* if there's no expiration date or it's not been reached yet, go ahead with displaying the post */ ?>
    < ?php require('post.php'); ?>
    < ?php endif; /* end not-expired test */ ?>
    < ?php endwhile; ?>

    Save this index.php and upload it to your theme folder, and see if your website works correctly. If it does, then you can go ahead and create a custom field value on the post’s editing page in the Dashboard.

    If it doesn’t work, replace the index.php file that isn’t working with the saved one that does work, and find somebody else to do it!

  3. I am using the blix theme and it doesn’t seem to recognize is there something I could put in place of that?

  4. Brent, perhaps you should contact the creator of the Blix theme and ask him. I believe it is Sebastian Schmieg.

    You could also try posting a question at the WordPress forum. Be sure to put “Blix” in your title and explain your problem clearly. Volunteers run the forum, so don’t be impatient if they don’t answer quickly.

    Good luck to you!

  5. it doesn’t recognize the php require(‘post’) on the blix theme (for some reason it didn’t show up on my last comment

Comments are closed.