Cache

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.

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.

How to check if related posts are cached

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 and superadmins 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(){
	km_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

	// MINUTE_IN_SECONDS
	// HOUR_IN_SECONDS
	// DAY_IN_SECONDS
	// WEEK_IN_SECONDS
	// YEAR_IN_SECONDS

	$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;
}

Caching posts manually

Try the Cache Extension or cache posts manually with a code snippet.

Example
This example caches 50 related posts for a widget. Let’s say you have a widget that shows 3 post thumbnails. The widget (and the shortcode) uses this function to fetch the related posts. To cache the same posts as the widgets we need to use the same parameters. An easy way to find out what parameters are used is to enable the debug class bundled with this plugin. Put this in your (child) theme’s functions.php:

add_filter( 'related_posts_by_taxonomy_debug', '__return_true' );

This adds a debug link to all widgets and shortcodes and will display debug information to admins only in your site’s footer.
Screen Shot 2015-08-08 at 13.03.01

Click the link and find the sections called “taxonomies” and “function args”.
Screen Shot 2015-08-08 at 13.27.49

If you compare the function args used with the defaults from the function you see only “post_thumbnail” and “posts_per_page” is needed to cache the 3 posts for a widget.

Here is the code to cache 50 related posts at once.

// Warning: Don't use this on every page load
// Be aware that it's expensive to cache posts at once.

// Get first 50 published post ids.
$posts = get_posts( 'posts_per_page=50&fields=ids' );

// Cache arguments. Same as the widget uses.
$args = array(
	'posts_per_page' => 3,
	'post_thumbnail' => true,
);

// Taxonomies. Same as the widget uses.
$taxonomies = array( 'category', 'post_tag', 'post_format' );

foreach ( $posts as $post_id ) {
	// Cache posts
	// Same arguments as used by the km_rpbt_related_posts_by_taxonomy() function.
	km_rpbt_cache_related_posts( $post_id, $taxonomies, $args );
}