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' );

#Disable caching entirely

This example disables caching completely, ensuring that access rules are checked in real-time for every query. Use with caution as this can impact performance.

function disable_query_filter_cache( $time ) {
	return 0;
}

add_filter( 'wpf_query_filter_cache_time', 'disable_query_filter_cache' );

#User-specific cache times

Set different cache times based on user roles or specific users. VIP users get real-time updates, while regular users get longer cache times.

function user_specific_cache_time( $time, $user_id, $post_types ) {
    
    $user = get_user_by( 'id', $user_id );
    
    if ( ! $user ) {
        return $time;
    }
    
    // VIP users get real-time access updates
    if ( in_array( 'vip', $user->roles ) ) {
        return 0;
    }
    
    // Premium users get 30-second cache
    if ( in_array( 'premium_member', $user->roles ) ) {
        return 30;
    }
    
    // Regular users get 5-minute cache
    return 5 * MINUTE_IN_SECONDS;
}

add_filter( 'wpf_query_filter_cache_time', 'user_specific_cache_time', 10, 3 );

#Post type specific cache times

Set different cache durations based on the content type being queried. Critical content gets shorter cache times.

function post_type_specific_cache_time( $time, $user_id, $post_types ) {
    
    $post_type = $post_types[0] ?? '';
    
    switch ( $post_type ) {
        case 'sfwd-courses':
        case 'sfwd-lessons':
            // LearnDash content - 30 seconds
            return 30;
            
        case 'product':
            // WooCommerce products - 2 minutes
            return 2 * MINUTE_IN_SECONDS;
            
        case 'download':
            // EDD downloads - 5 minutes
            return 5 * MINUTE_IN_SECONDS;
            
        default:
            // Default for other content
            return MINUTE_IN_SECONDS;
    }
}

add_filter( 'wpf_query_filter_cache_time', 'post_type_specific_cache_time', 10, 3 );

#Environment-based cache times

Use different cache durations for development, staging, and production environments.

function environment_based_cache_time( $time ) {
    
    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
        // Development - no caching for immediate testing
        return 0;
    }
    
    if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
        switch ( WP_ENVIRONMENT_TYPE ) {
            case 'staging':
                // Staging - short cache
                return 30;
                
            case 'production':
                // Production - longer cache for performance
                return 10 * MINUTE_IN_SECONDS;
        }
    }
    
    return $time;
}

add_filter( 'wpf_query_filter_cache_time', 'environment_based_cache_time' );

Was this helpful?