Use Custom Fields to Hide Stale Posts

Share this!

newspapers-in-driveway

The Problem

The other day, one of my clients asked me to delete a blog post that advertised a past event — one that had taken place several weeks earlier (June 27th of this year). The blog excerpt was still showing up at the top of the “Recent Posts” section of the home page because the client hadn’t published any newer blog articles that would have pushed it down or off the page.

Old news presented as new news is the Internet equivalent of going on vacation and letting newspapers pile up in your driveway… anyone who happens by will conclude you’re either lazy or not home.

My client was rightfully concerned that featuring a stale blog article on the home page might be perceived negatively. Old news presented as new news is the Internet equivalent of going on vacation and letting newspapers pile up in your driveway (yes, some people still get newspapers): anyone who happens by will conclude you’re either lazy or not home.

But deleting the blog article wasn’t a good solution. Every blog post — even those about past events — have marketing and SEO value.

The Solution: Use Custom Fields

So here’s a simple solution for hiding stale posts from your home page while keeping them alive and well in your blog.
custom-field-to-hide-stale-blog-post

  • I edited the post and created a custom field called expires, and I entered '2016-06-27' in the value box. Then I saved the post.
  • Then, in the front-page template, I modified the code in the loop.

Original Code

Here was the original code (the theme is Avenue). Lines 3 and 4 are the operative ones.

<?php while (have_posts()) : the_post(); ?>
	<?php
		if ('posts' == get_option('show_on_front')) {
				get_template_part('content', 'posts');
		} else {
			get_template_part('content', 'page');
		}
		if (comments_open() || '0' != get_comments_number()) :
			comments_template();
		endif;
	?>
<?php endwhile; // end of the loop.   ?>

New Code

And here is the new code:

<?php while (have_posts()) : the_post(); ?>
	<?php
		if ('posts' == get_option('show_on_front')) {
			// Check for an expirees date in the custom fields
			$test_meta = get_post_meta(get_the_ID(), 'expires', true);
			// Show the post if there is NO expires value or if it is earlier than today
			if ( empty($test_meta) || $test_meta > date('Y-m-d') )  {
				get_template_part('content', 'posts');
			}
		} else {
			get_template_part('content', 'page');
		}
		if (comments_open() || '0' != get_comments_number()) :
			comments_template();
		endif;
	?>
<?php endwhile; // end of the loop.   ?>

Line 5 uses the WordPress get_post_meta() function to look for a custom field with the name expires for each post. The get_the_ID() parameter retreives the post ID of the current post in the loop. The second parameter is the key to search for. The third parameter determines whether WordPress returns a single value (when set to true, which is what we want here) or an array (the default, false). The result is stored in the $test_meta variable.

The code on line 7 stipulates the condition for displaying the post content. Namely, WordPress will display:

  1. any post that has no custom field named expires ($test_meta is empty); and
  2. any post that does have a custom field named expires whose value is later than today.

Conclusion and Future Improvements

Custom fields in WordPress (meta-data) can be extremely handy for controlling the display of your blog posts. The challenge cited in this article is only one of many circumstances for which custom fields can be a solution.

One of the problems with custom fields is how user-unfriendly they are. The native interface for adding custom fields is not particularly intuitive, expecially for “civilians” who mantain their own site content. The Web site that’s the subject of this article happens to be one that I manage for my client. If and when personnel from his company start doing their own blog publishing — and maybe even before that — I’ll probably clean things up a bit by creating custom meta boxes. If I do, I’ll post a follow-up.

If you have your own examples of how you use custom fields — or you have comments or questions about how I implemented this solution — feel free to comment below.

Share this!

About JeffCohan.com

The nSiteful Tech Blog (the official blog of nSiteful Web Builders, Inc. since January of 2013) is where I (Jeff Cohan) and (occasionally) associates will be posting articles of potential interest to like-minded techies, nSiteful clients who are playing active roles in the maintenance of their own Web sites and blogs, and pretty much anyone interested in how Web strategies and tools can help them reach their goals.

This entry was posted in Techniques and tagged , by Jeff Cohan. Bookmark the permalink.

About Jeff Cohan

Jeff and his wife, Margie, are the proud parents of Sarah and Jake. Jeff is the founder, president and chief cook and bottle washer of nSiteful Web Builders, Inc., a Web development and Internet Consulting firm. In his spare time, Jeff builds Web sites and Web applications, plays guitar, putters around in his basement woodworking shop, mercilessly spoils his grandchildren, and creates videos from more than two decades of home movies. His current video project is an extended montage of people (mainly family members) asking him to stop filming them.

Leave a Reply

Your email address will not be published. Required fields are marked *