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() ) {
      $key = apply_filters('fragment_cache_prefix','fragment_cache_').$key;
      $output = get_transient($key);
      if ( empty($output) ) {
        $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?

  • 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 Feb 19, 2016 at 14:36
  • you need to implement save_post and delete_post hooks IMO.
    – knif3r
    Jul 6, 2016 at 9:37

1 Answer 1


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 that you have read and understand our privacy policy and code of conduct.

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