• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
    • Resources
    • Partner With Me
    • Frequently Asked Questions
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • At-A-Glance
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
    • Projects
  • Contact
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours
    • Book a Meeting with Jeff
    • Tell Me About Your Web Project
    • nSiteful Newsletter Archives
    • nSiteful Rewards

By Jeff - 8/1/2016
Cats: Techniques · Tags: Custom Fields, WordPress

techniques

Use Custom Fields to Hide Stale Posts

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.

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.

Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.

Related Posts

  1. Automatic “Visit Web Site” Links on WordPress Portfolio Posts
  2. How to use Advanced Custom Fields with Genesis
  3. Dive Into WordPress Custom Post Types – Part 1
  4. Dive Into WordPress Custom Post Types – Part 2
  5. Dive Into WordPress Custom Post Types – Part 3

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Post-Specific CSS for WordPress October 5, 2026
  • You built an online course, but they haven’t come. September 16, 2026
  • Custom MemberPress Pricing Pages December 19, 2025
  • CSS Data Attribute Override December 11, 2025
  • MemberPress Rules Using Custom Taxonomies and Custom Roles December 10, 2025

Filter By Category/Tag

Categories

  • Case Studies (8)
  • For Staff (1)
  • General (73)
  • Portfolio (7)
  • Reviews (14)
  • Snippets (24)
  • Techniques (52)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS Customer Service Custom Fields Custom Post Types Diagnostics Domain Names Facebook FooGallery Genesis Hosting HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses Membership Sites Mobile-Friendly MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management SEBA SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Twitter Video Web design Web forms WordPress

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

Book Meeting
770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2026, nSiteful Web Builders, Inc.

Cookies Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}
https://iframe.mediadelivery.net/embed/392008/42d18bc1-2adc-4741-b733-053d08d09c32
https://vz-000c5976-3ab.b-cdn.net/42d18bc1-2adc-4741-b733-053d08d09c32/play_720p.mp4

Receive occasional emails from Jeff