• I allow users to update a particular custom post type in the frontend.

    It appears that, when this happens, the cache for the post in question is not automatically cleared.

    How do I get the cache to clear for one specific post? I can do this in the backend, but I want to trigger this automatically, or trigger this via a function call.

    Can this be done?

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

    (@rockfire)

    Hi @mastababa

    Thank you for using our plugin!

    Are you using a custom endpoint for this post type or de default WordPress endpoint (i.e. /wp-json/wp/v2/<post-type-slug>)?
    In the last situation the cache should automatically clear, unless you are using something different than the default WP function (like wp_update_post or update_post_meta ) for updating the post. Or if you are filtering the output of the endpoint using the _fields parameter, in that case you should include the fields id and type (or id and taxonomy in case of a taxonomy-endpoint). These fields are needed for our plugin to detect the correct object type of the items in the endpoint.
    In case you are using a custom endpoint, you probably have to help our plugin detect the items inside the endpoint (see our FAQ)

    Thread Starter Babak Fakhamzadeh

    (@mastababa)

    Thanks.

    Indeed, when I need the cache to clear is when I’m using a custom Ajax call, not a default endpoint or function.

    I did have a look at your FAQ, but I could not find something that matched my use-case.

    My custom Ajax calls call ACF’s update_field() function. So, are you implying that if I follow my call to update_field() with something like the below, the cache is automatically cleared?

    $my_post = array(
          'ID'           => $post_id,
      );
    wp_update_post( $my_post );

    Or do I actually have to update something? So, I perhaps need to do this:

    $my_post = array(
    'ID' => $post_id,
    'post_title' => $temp_title
    );
    wp_update_post( $my_post );
    $my_post = array(
    'ID' => $post_id,
    'post_title' => $actual_title
    );
    wp_update_post( $my_post );

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi,

    I wouldn’t do a wp_update_post if there is no need to actually update it. Instead you can tell our plugin to clear the related caches:
    \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $post_id, $post_type );

    Thread Starter Babak Fakhamzadeh

    (@mastababa)

    Thanks. That’s indeed the kind of thing I was looking for.

    However, I’m not getting quite the result I am expecting.

    In fact, I now discover that, even after deleting all REST cache, I’m still handling responses that seem to be cached in the browser, as no new cache is generated after requesting that for which cache was just cleared. :/

    Plugin Author Richard Korthuis

    (@rockfire)

    @mastababa Since there is no new cache generated it seems your call isn’t coming through to WordPress. Unfortunately htere isn’t much our plugin can do in this case.

    Thread Starter Babak Fakhamzadeh

    (@mastababa)

    Yes, that’s indeed what I’m seeing. With the plugin turned on, the REST responses seem to be cached in the browser. With the plugin turned off, they are not. I had a similar problem earlier, and tried to ameliorate this through CloudFlare, but I remember that I could not get that to work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Delete the cache for one URL?’ is closed to new replies.