Hook to clear the object cache

I noticed that the “Object cache for parsed and pre-processed template posts” option is enabled by default. While this improves performance, it can sometimes cause issues during development or dynamic updates.

Would it be possible to expose a hook or public function to programmatically clear this cache? For instance, something like:

php

Copier

do_action('tangible_clear_template_cache');

This would be helpful for developers who want to manually or conditionally reset the cached output for specific templates or globally.

@eliot any thoughts about this ?

public function to programmatically clear this cache

Here’s the function to delete the cache for a specific template.

use tangible\template_system;

template_system\delete_processed_template_post_cache( $post );

The use statement should be at the top of the file, after any namespace declaration. The delete cache function expects a WP_Post object, so if you have only the ID, you can pass get_post( $id ).

There’s no function to delete cache for all templates, due to the way the cache key (name) is generated from the template post ID. It means there’s no direct way to retrieve all existing cache keys. Instead, you’ll need to get all template IDs, loop through them and remove cache (if any) for each post.

$ids = get_posts([
  'post_type' => 'tangible_template',
  'posts_per_page'  => -1,
  'fields' => 'ids',
]);

foreach ($ids as $id) {
  template_system\delete_processed_template_post_cache(
    get_post( $id )
  );
}

…reset the cached output for specific templates or globally

To refresh the cache:

use tangible\template_system;

// ..After updating a template..

template_system\process_and_cache_template_post( $post );
1 Like

It works perfectly ! Thank you so much :+1:

1 Like