If Post is Older Than

WordPress-Christmas-12
12th door of our Advent Calendar, we are half through! Sometimes you need a query that satisfies the requirement that the advertising is displayed only if the article has reached a certain age. Who does not want to rely on a Plugin, we recommend the following little solution.

The WordPress conditional tags are useful tools for various queries, and we can also create our own functions that perform the same task. The following small function belongs to the functions.php of the theme and can be used everywhere.

function is_old_post($days = 5) {
	$days = (int) $days;
	$offset = $days*60*60*24;
	if ( get_post_time() < date('U') - $offset )
		return true;
	
	return false;
}

The new „Conditional Tag“ can then be used as follows.

if ( is_old_post(10) ) {
	// do something if the post is old
} else {
	// do something if the post is not old
}

The function uses as default 5 days and with the help of the parameter it can be adjusted. This opens different oportunities to start a query and use the age of the post.