Support » Plugin: WP REST Cache » Issue when _fields param is used

  • Resolved Nick Papazetis

    (@papazetis)


    The cache is not flushed or cleared on post save/update when the endpoint has _fields param

    The issue affects CPTs as well

    /wp-json/wp/v2/posts?_fields=id

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @papazetis

    Thank you for using our plugin!

    That is correct, our plugin needs at least the fields id and type (or id and taxonomy in case of a taxonomy endpoint) to determine the correct object type. If it can’t determine the object type correctly it cannot flush it when that object type is saved/updated.
    To determine the relations within a cache it would need even more fields to work correctly. It would need a combination of fields (which exactly depends on the endpoint and the object type within it):

    • id and post_type
    • id, type and ( slug or status )
    • id, slug and _links
    • taxonomy, id, name and slug
    • taxonomy and term_id

    If you want to keep the number of fields in the output limited to less fields than the ones mentioned above, you would have to help the plugin determine the correct object type (using the wp_rest_cache/determine_object_type filter) and the correct relations (using the wp_rest_cache/process_cache_relations)

    Some small examples of how to use the filters:

    /**
     * Help determine the correct object type for our custom endpoint.
     *
     * @param string $object_type The object type as determined by the plugin.
     * @param string $cache_key The cache key for the current cache record.
     * @param mixed $data The data that is to be cached.
     * @param string $uri The requested URI.
     *
     * @return string
     */
    function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    	if ( $object_type !== 'unknown' || strpos( $uri,  'MY_NAMESPACE/v1/MY_ENDPOINT' ) === false ) {
    		return $object_type;
    	}
    	// Do your magic here.
    	$object_type = 'website';
    	// Do your magic here.
    	return $object_type;
    }
    
    add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );

    and:

    
    /**
     * Process cache relations.
     *
     * @param int    $cache_id The row id of the current cache.
     * @param mixed  $data The data that is to be cached.
     * @param string $object_type Object type.
     * @param string $uri The requested URI.
     */
    function wprc_process_cache_relations( $cache_id, $data, $object_type, $uri ) {
    	if ( ! isset( $data['data'] ) || false === strpos( $uri, '/namespace/v2/custom_endpoint' ) ) {
    		return;
    	}
    
    	$caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
    
    	if ( isset( $data['data']['id'] ) ) {
    		// Single item.
    		$caching->insert_cache_relation( $cache_id, $data['data']['id'], 'my-post-type' );
    		return;
    	}
    
    	foreach ( $data['data'] as $record ) {
    		if ( ! is_array( $record ) || ! isset( $record['id'] ) ) {
    			continue;
    		}
    		$caching->insert_cache_relation( $cache_id, $record['id'], 'my-post-type' );
    	}
    }
    
    add_action( 'wp_rest_cache/process_cache_relations', 'wprc_process_cache_relations', 10, 4 );
    Thread Starter Nick Papazetis

    (@papazetis)

    Hi @rockfire.

    Thank you for your reply and congrats for this great plugin.

    The type field worked fine for me.

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Issue when _fields param is used’ is closed to new replies.