While working on a website, the client needed the homepage to function differently from other websites and blogs.

Rather than having the home page show the last x number of posts or editing the loop through query_posts to only show posts within a certain timeframe, the posts would either need to appear on the homepage until:

  • a specific date – and then the post will either be archived or deleted
  • or if no date is set, the post should appear for a month

I knew that there was a way to makes posts “expire” by setting a date and time as a custom field and editing the loop. A search on Google reveals several articles (all of which are identical – exactly the same, so I don’t know the original article which I could link to). I then also found an article that edited the expire posts code so that the post would auto delete rather than expire: Modify you WordPress theme to enable an expiration date for your posting.

There a few drawbacks to the methods I found:

  • they needed post custom fields, which aren’t the most user friendly interface for end users to employ
  • custom fields do not look nice
  • the custom field entry needed to be in the format mm/dd/yyyy 00:00:00 not exactly user friendly nor easy to understand (is 17:00:00 7pm or 5pm??)
  • posts would either only expire (be archived) or be deleted

So now I needed to put on my PHP hat and find a way to get what needed to be done and avoid the drawbacks listed above. I knew right away that I would need to use custom write panels to make everything easy to look at and use. I wrote about using them here, WordPress Custom Write Panels & prioritizing what to display and used this as my reference, Revisited: Creating Custom Write Panels in WordPress

In the end, this is what my WordPress custom write panel looked like:
WordPress make posts expire or auto-delete

Obviously, this is for posts in the “announcements” category. Anyway, open up your themes functions.php (or make one) and add this code in: