Code Stuff

POST EXPIRE

One of my sites has two WordPress loops on the front page. At the top of the page is a "featured" section which displays the one most recent post from a certain category. A second loop is below that which displays all the posts in every category, except the one post already shown in the featured spot. (We use the code from this page of the WordPress site, under "Multiple Loops in Action".)

Originally, this featured section of the page was there all the time. So even if its last post was 12 days old and the loop below had new content, it was up there making our page look stale. Using the code below, we now remove the feature loop automatically once the post in it reaches a specified age. That age can be defined on a per post basis, and there is a fall back when no age is given by the writer.

Here is how it works. On the editing page in WordPress, one of the custom fields we use is called "post-expire". The writer can enter a number here that is the number of days he wants the blog post to remain in the top featured spot before expiring and moving to the loop below. If no number is entered the default is 10. This is the first loop, for our featured post:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<?php $my_query = new WP_Query('cat=22&showposts=1&caller_get_posts=1'); ?>
<?php if (have_posts()) : ?>
<?php while ($my_query->have_posts()) : $my_query->the_post();
	$do_not_duplicate = $post->ID; ?>

<?php $post_expire = get_post_meta($post->ID, 'post-expire', true); 
	if ($post_expire == "") { $post_expire = 10; }
	$goAwayDate = time() - (60 * 60 * 24 * ($post_expire));
	$postDate = get_the_time('U');
	if ($postDate < $goAwayDate) { $do_not_duplicate = ""; } 
	else { ?>

<!-- your post code -->

<?php } ?>
	
<?php endwhile; endif; ?>

Line 1 starts the first loop, shows only the first post from category 22, and ignores stickiness. Line 4 sets a variable called "$do_not_duplicate" to the current posts's ID. Later we will tell the second loop to skip any post with this ID.

Line 6 sets a variable called "$post_expire" to the number the writer puts in our custom field. If empty, line 7 sets the default to 10 days.

Line 9 says what to do if the post is past its expiration date. Here I've set the "$do_not_duplicate" variable back to empty. This loop will then end and start the next loop.

Line 13 is where you put your code to display the post if it is still "fresh, before the expiration date.

Now we start our second loop:

 1
2
3
4
5
6
7

<?php if (have_posts()) : while (have_posts()) : the_post(); 
	if( $post->ID == $do_not_duplicate ) continue;
	update_post_caches($posts); ?>

<!-- your post code -->

<?php endwhile; endif; ?>

Line 1 starts the second loop.

Line 2 checks to see if the post ID for the current post in this loop is the same as the variable we set in the first loop. If so, it skips this post and moves on to the next. (When the featured post in the first loop expires, we empty the "$do_not_duplicate" variable so it now will show up in this loop.)

That's it! Two loops, no duplicate posts, and you can make the first loop disappear after a certain number of days.