0

I have fragment caching implemented in a page. I've got the codes from https://css-tricks.com/wordpress-fragment-caching-revisited/.

Which goes like this:

    function fragment_cache($key, $ttl, $function) {
      if ( is_user_logged_in() ) {
        call_user_func($function);
        return;
      }
      $key = apply_filters('fragment_cache_prefix','fragment_cache_').$key;
      $output = get_transient($key);
      if ( empty($output) ) {
        ob_start();
        call_user_func($function);
        $output = ob_get_clean();
        set_transient($key, $output, $ttl);
      }
      echo $output;
    } 

And I want to clear the transient set when a post is added. How can i do that?

2
  • because i have this taxonomy.php that loads all the post under it. The looping was within the fragment cache. I just thought that if i add new post under that taxonomy, it won't show the new post because the time has not yet elapse. Is that how it works right? or i am mistaken? @N00b Help me Commented Feb 19, 2016 at 14:36
  • you need to implement save_post and delete_post hooks IMO.
    – knif3r
    Commented Jul 6, 2016 at 9:37

1 Answer 1

0

Add the below code in your active theme's functions.php file.

function wpse_delete_query_transient( $post ) { // Deletes the transient when a new post is published delete_transient( 'd_results' ); } add_action( 'new_to_publish', 'wpse_delete_query_transient' );

This will delete the transient every time when a new post is published.

If you want to delete the transients on differrent post status transitions, you may refer codex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.