fbpx

wpf_query_filter_cache_time

#Overview

WP Fusion’s Filter Queries option allows you to modify the results of database queries to exclude content that a user can’t access (based on WP Fusion’s access rules).

When used in Advanced mode, Filter Queries will build up a list of posts a user has access to before the main query is run, and add it to the main query via the post__not_in parameter (in order to exclude any restricted posts).

That list of post IDs is then cached for the current user and post type, to avoid running the same query multiple times in quick succession (which can be quite slow).

This uses wp_cache_set() with a default expiration time of one minute. This can work in two ways depending on your server setup:

  1. No object caching: If you are not using any object or memory caching (like Redis), the cache will last for the current page load. That means the same query will run at most once per page load.
  2. Object caching: If you use Redis, Memcached, or another object caching solution (we use Redis Object Cache), the list of restricted post IDs will be cached in memory for one minute, even across page views.

The wpf_query_filter_cache_time filter lets you modify the length of time until the cache expires

#Parameters

  • $seconds (int): The number of seconds until the cache expires. Default 60.
  • $user_id (int): The user ID for whom the posts are being cached
  • $post_types (array): The post types used in the query. The first post type is used in the cache key.

#Examples

#Extend the cache time

This example extends the cache time to one hour. It will result in fewer slow database queries, but it means that if new content becomes unlocked for a user, it could take up to an hour for it to become visible.

function extend_query_filter_cache_time( $time ) {
	return HOUR_IN_SECONDS;
}

add_filter( 'wpf_query_filter_cache_time', 'extend_query_filter_cache_time' );

Was this helpful?