Modify you WordPress theme to enable an expiration date for your posting

WordPress is a great application for people who want to set up their own blog, product display platform or even a whole site. It comes with many nice features and there is a very large community of developers who are contributing their strength to make it better and better. However, I have to say, WordPress is still not almighty yet.

One of my friends just set up a new WordPress and he is going to share the shopping tips in Vancouver area on his blog. Usually when we use WordPress, we tend to keep the post published and accumulate more and more posts; however, this is not the case for him. He need to share the deals or other shipping tips on his site, but most of those information has expiration on it. For example, most of the sale price will last about one week and there will be daily sales that could only last for 24 hours. When the blog first started, it’s pretty easy to manage; just find out the posts that are not valid any more, simply put them into “Draft” or “Trash”. However, as the site growing, the management is becoming chaos when sometimes he forgets to delete a post.

Failed trials with plugins available:

So I googled for him and hopefully I could find a plugin that would handle the job for him.

Comparing to Twitter plugins, there are very very few for expiration. The closest ones we could find are “Post Expirator” and “Auto Delete Posts”.  After we tested them out, I found these two plugins wouldn’t work for his site very well.

  • The “Post Expirator” doesn’t work with WordPress 2.9.1 very well;
  • The “Auto Delete Posts” was nice when we read the description. However, when we actually read the FAQ, we found that the plugin will only work when there is new post created or when a post is being edited. Since my friend is not going to be sitting there all the time editing posts or writing posts, especially most of the sales end in the middle of the night.

Q-1) How do I use this plugin?

A-1) The Auto-Delete-Posts plugin will be run each time a new post or page is created or when an existing post or page is edited.

Since I know a bit of PHP, I start looking at other ways to solve this problem — the coding way.

Trick the theme and not displaying expired post in front page:

According to what I know, there is a while-loop in every theme’s main page to display the posts that are available to be shown. So my first thought was to set an expiration time in the custom filed when writing the post. In the while-loop of the theme, if WordPress checked the time set in the post is older than the current time, then just don’t show it.

I accidentally found someone who posted the code online too; if you are interested, you can visit How to: Set post expiration date/time on your WordPress blog.

PHP Code:

$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For exemple…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

To create a post with date/time expiration, just create a custom field. Give it expiration as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.
The post will not show after that time stamp.

This methods works very well on the front page. However, just a minute after we thought we did it, we found the post is still there when we clicked the category of the post we didn't see in the front page. What the PHP code does was rally just hide those expired post in the home page. So if you have the link to the post or if you go to Archive, you will still be able to see it. In this case, it doesn't help that much, so I will need to do some more extra work to achieve what we wanted to do based on the code wprecipes.com provided.

More “hacks” on the theme to get it work:

Before we moved to the next level, let me first explain how the PHP code above works. In this post, I will use WordPress’ default theme to explain.

< ?php while (have_posts()) : the_post(); //Delete ‘}’ here
$expirationtime = get_post_custom_values(‘expiration’);
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time()+28800;
if ( $secondsbetween > 0 ) {//If time difference >0, then display post

id=”post-< ?php the_ID(); ?>”>

< ?php the_time(‘F jS, Y’) ?>

< ?php the_content(‘Read the rest of this entry »’); ?>

< ?php the_tags(‘Tags: ‘, ‘, ‘, ‘
‘); ?> Posted in
< ?php the_category(‘, ‘) ?> | < ?php edit_post_link(‘Edit’, ”, ‘ | ‘);
?>  < ?php comments_popup_link(‘No Comments »’, ’1 Comment »’,
‘% Comments »’); ?>

}

< ?php endwhile; ?>[/php]

The red part is where you should put the code into your theme, basically before displaying the post out in the while loop, you will just need to check if the time has already expired. Just a note that, this code will read the time from your server, not your WordPress setting. So even though my friend’s WordPress was set to Vancouver(GMT-8) time zone, it’s always 8 hours delayed. The blue part just adjust the time back to the Vancouver time zone. If you are in a different time zone, just do a simple math. Let say, you are in GMT +7 time zone, you are ahead of time, so instead of “+”, you will need to “-” the difference, 7 hours = 7 * 60 *60 = 25200; there for you will need to put “-25200″ for the blue part.
So we set the post invisible IF the time difference is greater than 0, but what if ELSE? This is what I am going to do. I am going to delete that post.
Reference to the WordPress Codex, the function to delete a post is as following:

<?php wp_delete_post( $postid, $force_delete = false ) ?>

So what I need is really the ID of the post. And to find out the post ID is not a difficult task, here you go:

<?php

function get_postID() {

global $post;

$thePostID = $post->ID;

return $thePostID;

}

?>

I put this function just right before the while loop, and for the ELSE, I added this line in:

<?php wp_delete_post( get_postID(), false) ?>

And the job is done. What the code do is that, if you set an expiration date to your post, the post will be checked; if expired, it will be deleted, otherwise, it will be displayed. For the all the parts I have made changes, I colored them out here:

<?php
function get_postID() {
global $post;
$thePostID = $post->ID;
return $thePostID;
}
?>//Function to get post ID
<?php while (have_posts()) : the_post();  //Delete '}' here
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time()+28800;
if ( $secondsbetween > 0 ) {//If time difference >0, then display post
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author()
?> --></small>

<div>
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in
<?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | ');
?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;',
'% Comments &#187;'); ?></p>
</div>
}else{//If not, then delete it.
<?php wp_delete_post( get_postID(), false) ?>
}
<?php endwhile; ?>

Post Note:
It’s a simple “hack” to WordPress theme. You could also try that with your theme if you would like to have the same functionality. I will also try to make this one into a plugin unless someone else made it before I do; hopefully this post could help you out. You may contact me if you have problem to modify your theme and get the script work for your WordPress.

Filed Under: Programming

Tags:

RSSComments (1)

Leave a Reply | Trackback URL

  1. gan says:

    Thanks for the sharing.I had tried to use wprecipie code as well.So far i found ok.But still got something need to check,not sure you got any clue.
    1)I want to maintain the expired post,but when the code load,it will filter the expired post and only list the valid post.But when i put in the pagination plugin (wp_pagenavi),it doesn’t showed the correct pagination.I believe it still load all the post and calculate the total page.Do you have any idea how to fix this?
    2)How to know which time zone did the webhsoting use?

    Looking forward to hear from you,
    Thanks.

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.