• Resolved reggieofarrell

    (@ofarrellaudio)


    Hello. I have a custom post type called downloads that I have created a custom REST controller for. I have registered the endpoints with the wp_rest_cache/allowed_endpoints filter. The caching is working. I need to clear that cache whenever a downloads post is updated though. If my downloads related endpoints are all grouped under vc/v1/downloads , how would I programmatically clear everything under vc/v1/downloads when a download is updated?

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

    (@rockfire)

    Hi @ofarrellaudio,

    Thank you for using our plugin!

    On the cache overview page (Settings > WP REST Cache > Endpoint API Caches) you see the object type for your endpoint is unknown, which means our plugin is unable to detect your endpoint contains items of the downloads post type.

    See our FAQ on how to help our plugin determine the correct object type (i.e. downloads). Besides that you should also help it determine the cache relations using the action wp_rest_cache/process_cache_relations, see an example here on how to use it. If the object type is set correctly and the relations are determined, our plugin will automatically clear the correct caches if a download is updated.

    Another option would be to have your endpoint contain a field id and a field post_type for each download in the response. That way our plugin would be able to detect the object type and the relations correctly.

    Thread Starter reggieofarrell

    (@ofarrellaudio)

    Thanks @rockfire. There is only one endpoint in this group currently and it fetches the downloads in a particular taxonomy by the term slug and transforms the data for each download before returning the results. So for example if the endpoint is…

    vc/v1/downloads/get-by-content-group/{term-slug}

    I need a function that can be called from anywhere that can clear the cache for that particular endpoint whenever a download is created or updated that is in a particular content-group. I know how to hook into post creation and update and do some logic there to determine the taxonomy and term. Is there a function I can call from my own code to clear the cache for a specific endpoint though? If not, is there one I can call from anywhere to clear everything under vc/v1/downloads/get-by-content-group/ or even just everything under vc/v1/downloads/?

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @ofarrellaudio,

    Yes, there is a function you can use: \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_instance( $endpoint, $strictness, $force );

    See the function documentation on how to use it:

    /**
     * Delete caches by endpoint path.
     *
     * Delete caches by an endpoint path either strict (\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_STRICT)
     * matching only this exact same endpoint path (and query params), params (\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_PARAMS)
     * matching the exact same endpoint path with any query params that might have been used, or loose
     * (\WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_LOOSE) matching any cache that was called starting with
     * the endpoint path (ignoring any query params or subpaths following the given path).
     *
     * @param string $endpoint The endpoint path to match.
     * @param string $strictness The match type. (Can be either \WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_STRICT, \WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_PARAMS or \WP_Rest_Cache_Plugin\Includes\Caching\Caching::FLUSH_LOOSE).
     * @param bool   $force Should the caches be deleted ($force = true) or just flushed ($force = false).
     *
     * @return bool|\WP_Error
     */
    Thread Starter reggieofarrell

    (@ofarrellaudio)

    Thanks @rockfire! That looks like what I’m looking for.

    Plugin Author Richard Korthuis

    (@rockfire)

    Luckily you were able to solve your issue, although I made a msitake in my comment. The function is delete_cache_by_endpoint and not delete_cache_by_instance (just for people who find this thread)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Clear custom endpoint cache when custom post type is saved’ is closed to new replies.