• Resolved juliamb

    (@juliamb)


    Hi!

    I’ve added this code to force cache cleaning when an specific CPT is created/updated:

    function clean_cache_for_cpt( $post_id, $post, $update ) {
      if ( 'cpt' !== $post->post_type ) {
          return;
      }
     
      WPO_Page_Cache::delete_single_post_cache($post_id);
    }
    add_action( 'save_post', 'clean_cache_for_cpt', 10,3 );

    But it’s not working, I cannot see the changes until I manually clean the cache.

    The problem is that I have a custom query on a homepage to list specific custom posts but when I update any of them, the changes are not reflected until I force the cache to be cleaned.

    I don’t want to set the homepage not to be cached, but I need to clear the cache for this CPT.

    Any suggestion?

    Thanks in advance, regards.

    • This topic was modified 3 years, 2 months ago by juliamb.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter juliamb

    (@juliamb)

    I also tried this code but it’s not working either:

    add_action('wp',
       function() {
         global $post;
         if ('cpt' == $post->post_type && !defined('DONOTCACHEPAGE')) {
           define('DONOTCACHEPAGE', true);
         }
       }
     );
    Thread Starter juliamb

    (@juliamb)

    [UPDATED] I thought it was not working just on mobile but that is not true, it’s not working in general.
    I’ve updated the topic.

    Thanks, regards.

    @juliamb

    You’ll have to use the filter outside the hook function call.

    add_filter( 'wpo_purge_all_cache_on_update', '__return_true' );

    So it will purge all the cache whenever you update the post/page or create a new post/page.

    Thread Starter juliamb

    (@juliamb)

    Hi!

    Thanks for the response. I’ve tried that hook but it’s not working, I have to manually clear the cache to see the changes of a custom post type that is queried in the home page.

    The home page is built with Elementor and the block that list these custom posts is a custom Elementor widget, maybe this has something do to with the issue?

    Thanks, regards.

    @juliamb It seems to be compatibility issue with Elementor blocks, I’ll report this issue to our development team so it can be fixed in future release.

    Till then you’ll have to manually purge the cache from WP-Optimize > Cache

    Thread Starter juliamb

    (@juliamb)

    Ok, I’ll wait for future releases then.

    Thanks for your response!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Forcing cache clean by code not working’ is closed to new replies.