Last updated October 8th, 2018 at 07:01 pm
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.
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.
- 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:
- any post that has no custom field named expires ($test_meta is empty); and
- 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.
Leave a Reply