Categories
Tutorial WordPress

If post hasn’t been modified recently in WordPress

Today an issue came up where a reader of one of a site I manage was referring to content that was out of date. Normally that’s not a problem but in this case it is a timely pandemic documentation page where the content is very likely to be out of date within a couple months if its not routinely updated.

To solve this challenge I decided to write a small function that will check the last modified date of a post and place an alert below the title and above the content. That notice would read…

This page has not been updated in 60 days. The information below may be outdated.

Which is a casual way of alerting the reader that if this information is timely perhaps check elsewhere on the site for a newer post. Likewise, if that person is looking for an older post they can safely disregard the notice. posting a discrete notice can come in handy depending on the context that brought a reader to the post.

Functions.php

<?php
// If post is older than 60 days return true
function jd_is_old_post($days = 60 ) {
	$days = (int) $days;
	$offset = $days*60*60*24;
	if ( get_post_modified_time() < date('U') - $offset )
		return true;
	
	return false;
}
?>

Place within the WordPress Loop

<?php
// If post is older than 60 days display this notice
if ( jd_is_old_post() ) {
	echo '<div class="old-post-notice">';
        echo '<p><strong>This page has not been updated in 60 days. The information below may be outdated.</p></strong>';
    echo '</div>';
} 
?>

By Joseph Dickson

Joseph Dickson is a web developer in higher education and an avid proponent of using WordPress’ core features to create flexible and manageable design solutions.