This page is part of the plugin documentation for the Related Posts by Taxonomy plugin.

Note: This is an advanced topic. If your site is already cached (by a plugin or other means) or have limited knowledge about php it’s best to leave the caching of related posts alone. This plugin has a very optimized (object cached) query to get the related posts to begin with.

Tip: See query optimizations before using this feature.

Default cache

This plugin uses the WordPress object cache by default to cache related posts. This cache is non-persistent. This means that data stored in the cache resides in memory only and only for the duration of the request. Object cached data is not stored persistently across page loads.

Front end persistent cache

This plugin supports a front end persistent cache layer from version 2.1.0 The persistent cache stores the related posts across page loads.

You can enable it by putting this in your (child) theme’s functions.php file.

add_filter( 'related_posts_by_taxonomy_cache', '__return_true' );

If enabled the plugin tries to get the related posts for a widget or shortcode from the persistent cache first. If it can’t find it in the cache, it gets them normally and stores (caches) the related post ids as post meta. This is done in the front end of your site whenever a shortcode or widget is displayed. The next visitor to your site will get the cached related posts.

Cache Extension

If you need to have all posts cached at once use this Cache Extension plugin. This separate plugin will add a settings page where you can cache related posts in batches with AJAX. This extension will be updated every time this plugin is updated (with the same version number).

When to use the persistent cache

It’s up to you to decide what caching strategy to follow. The related posts cache is updated one widget or shortcode at a time (when a visitor visits a page of your site). The more visitors your site has, the more up to date the cache will be over time.

If your site is updated with new posts every few hours this cache will not benefit you as much because the cache is automatically flushed when updating, publishing or deleting posts, terms or post thumbnails. See Automatic Flushing of the cache

If you don’t mind cached related posts not being up to date you can disable automatic flushing. If flushing is disabled it’s possible the links go to non related posts where the (related) terms were updated/deleted. This cache will never link to a (deleted) post that doesn’t exist (404).

Sites that are not regularly updated will benefit by setting a high expiration time for the cached posts. The default expiration time for cached related posts is 5 days. After this time the cache is flushed. You can disable the expiration time for related posts by setting it to zero seconds.

Cache Log

To check if posts are cached put this in your (child) theme’s functions.php.

add_filter( 'related_posts_by_taxonomy_display_cache_log', '__return_true' );

This will display the cache log in the toolbar to logged in admins in the frontend of your site.
Screen Shot 2015-09-03 at 14.10.21
As you can see the related posts were not yet cached. Now, if you refresh the page you will see the related posts are cached.
cache log in toolbar

Automatic Flushing of the cache

The persistent cache is automatically flushed when:

  • a post is trashed, untrashed or deleted
  • post terms are updated (everytime you publish/update a post)
  • a post thumbnail is added, updated or deleted
  • the cache exceeds a time limit

See below how to disable the automatic flushing of the cache.

Disable automatic flushing

If you want to deploy your own strategy to flush the cache, or don’t mind if older posts do not have correct (newer) related posts you can disable the automatic flushing. Put this in your (child) theme’s functions.php

add_filter( 'related_posts_by_taxonomy_cache_args', 'rpbt_disable_cache_flush');

function rpbt_disable_cache_flush( $args ) {

	// Disable automatic flushing from actions
	$args['flush_manually'] = true; // bool.

	// Disable automatic flushing after a time limit
	$args['expiration'] = 0; // time in seconds (0 == no expiration).

	return $args;

Be aware that the cached related posts might not be correct after you update your site with new terms, posts etc.

Flush the cache manually

You can flush the cache manually with the km_rpbt_flush_cache() function in your (child) theme’s functions.php.
Note: Flushing the cache is an expensive action and should not be done on every pageload.
Use the km_rpbt_flush_cache() function in the wp_loaded action or later.

add_action( 'wp_loaded', 'rpbt_flush_cache', 11 );

function rpbt_flush_cache(){

Setting an expiration for cached posts

You can set an expiration time for the cached posts with this in your (child) theme’s functions.php. After the time expires the cache is flushed.

add_filter( 'related_posts_by_taxonomy_cache_args', 'rpbt_cache_set_expiration' );

function rpbt_cache_set_expiration( $args ) {

	// Use these constants


	$args['expiration'] = DAY_IN_SECONDS * 5; // 5 days.

	// Set it to zero if you don't want to set an expiration date
	// Cached related posts will not be flushed (from the database ) by expiration time.

	// $args['expiration'] = 0; // disable expiration time.

	return $args;

Cache Extension

Try the Cache Extension to cache all posts in one go.