Title: Temp disabling all caching within a specific function?
Last modified: January 4, 2022

---

# Temp disabling all caching within a specific function?

 *  Resolved [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/)
 * Hi all,
 * we have several functions which I start as admin on demand (using GET parameters)
   which act based on user-meta data.
 * We have seen lately that, because W3 is active, “old” metadata was used (because
   metadata uses WP Object caching) which kinda rendered those functions partially
   useless.
 * Do you “pay attention” if I simply switch wp_using_ext_object_cache(false) during
   runtime, is this a solution for WP Object Caching deactivation?
 * And how to change behaviour of your DB caching? Can I simply override the variable
   $cache_reject_reason and set it to a value != null, e.g. $wpdb->cache_reject_reason
   = ‘cronjob’? That should deactivate as well, correct?
 * I now created a function which disables object cache, sets all constants, adds
   a dbcan filter and also sets the value of the rejected_reason of the active processor
   within the DB-CachedQuery instance, what do you think? Is this complete?
 *     ```
       function temp_disable_all_w3cache()
       {
           # Disable WP Obj Caching ...
           wp_using_ext_object_cache(false);
   
           # Disable W3 Constants (not sure whether they are recognized during runtime)
           $cons = array(
                       'DONOTCACHEPAGE',
                       'DONOTCACHEDB',
                       'DONOTMINIFY',
                       'DONOTCDN',
                       'DONOTCACHCEOBJECT',
                       'DONOTCACHEPAGE'
                    );
           foreach($cons as $c)
           {
               if(!defined($c))
                   define($c, true);
           }
   
           # Add Filter w3tc_dbcache_can_cache_sql
           add_filter('w3tc_dbcache_can_cache_sql', function() { return 'DONT_CACHE_MY_CRONS'; });
   
           ########## CHANGE CACHE REJECT REASON for DB CACHING...    
           // Create the closure by reference 
           // https://stackoverflow.com/a/17560595/701049
           $reader = function & ($object, $property) {
               $value = & Closure::bind(function & () use ($property) {
                   return $this->$property;
               }, $object, $object)->__invoke();
               return $value;
           };
   
           global $wpdb;
           try {
               $active_processor = & $reader($wpdb, 'active_processor'); # GET property by reference ... to be able to change it
               $reject_reason = & $reader($active_processor, 'cache_reject_reason'); # GET property by reference ... to be able to change it
               $reject_reason = 'DOING_CRON'; # Change private property by reference -> modifies original WPDB
           } catch (Exception $e) {}    
       }
       ```
   
 * Thanks so much

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15218104)
 * Hello [@sx1001](https://wordpress.org/support/users/sx1001/)
 * Thank you for reaching out.
    Let me try and test this and check the behavior 
   and I’ll get back to you with more info. Thanks!
 *  Thread Starter [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15221683)
 * Hi Marko,
    thanks, I’ll wait for your feedback 🙂
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15221883)
 * Hello [@sx1001](https://wordpress.org/support/users/sx1001/)
 * W3TC does not check the global used by [https://developer.wordpress.org/reference/functions/wp_using_ext_object_cache/](https://developer.wordpress.org/reference/functions/wp_using_ext_object_cache/).
   WordPress may ignore the external object cache if it is disabled that way.
    You
   have a typo `DONOTCACHCEOBJECT` it should be `DONOTCACHEOBJECT`. That should 
   work if your function is run early enough in the WP loop of actions and filters.
   The `w3tc_dbcache_can_cache_sql` filter is used to set a reject reason. This 
   will not disable the DB Cache; it’s used for the comment in the debug HTML comment
   on pages. The closure by reference part may work — needs testing so you may want
   to check and test this on your end. I hope this helps! Thanks!
 *  Thread Starter [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15222176)
 * Hi Marko,
 * thanks for getting back to me.
 * The basic question is: Even if W3 does not consider wp_using_ext_object_cache,
   after I use it, does WP ignore the global object cache (i.e. W3TC) and therefore
   directly take everything from the DB instead of relying on wp_cache_set/add/get?
 * If not – than how else to disable Object cache during runtime?
 * Thanks!
 *  Thread Starter [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15224776)
 * Hi [@vmarko](https://wordpress.org/support/users/vmarko/),
 * I checked once more:
    During Runtime, somewhere in wp_head action, I called temp_disable_all_w3cache()
   and than I queried a value from the DB which I changed using Phpmyadmin beforehand.
 * Unfortunately the new value was not retrieved until I purged the W3 TC Cache 
   overall!
 * Please assist me, this is a MUST HAVE functionality, to be able to temporarly
   disable caching somehow. And be it in a ugly way by changing the wpdb back to
   its original Object -> the main thing, that this CAN be done.
 * Thanks in advance!
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15225726)
 * Hello [@sx1001](https://wordpress.org/support/users/sx1001/)
 * `define('DONOTCACHEDB', true);` should work, but has to be defined before the
   DB cache file is run.
    You can also try define( ‘W3TC_IN_MINIFY’, true ); to 
   disable everything. Thanks!
 *  Thread Starter [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15227897)
 * Hi,
 * unfortunately this does not work “somewhere” in the middle of the code during
   Runtime.
 * For us, this is a MUST HAVE feature, to be able to temporarly disable caching
   somehow DURING runtime, not via very early dropin disabling…
 * But if it’s simply not possible, alright than…
 *  Thread Starter [sx1001](https://wordpress.org/support/users/sx1001/)
 * (@sx1001)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15227954)
 * I’m not 100% sure yet, but I think, this did the trick:
 *     ```
       # Disable WP Obj Caching ...
       wp_using_ext_object_cache(false);
       wp_cache_flush();
       wp_cache_init();
       ```
   
 * All subsequent function calls will have an EMPTY WP Object Cache, hence re-reading
   from DB.
 *  Plugin Contributor [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * (@vmarko)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15235331)
 * Hello [@sx1001](https://wordpress.org/support/users/sx1001/)
 * Thank you for the information.
    Glad to know you managed to find the solution.
   Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Temp disabling all caching within a specific function?’ is closed to 
new replies.

 * ![](https://ps.w.org/w3-total-cache/assets/icon-256x256.png?rev=1041806)
 * [W3 Total Cache](https://wordpress.org/plugins/w3-total-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/w3-total-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/w3-total-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/w3-total-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/w3-total-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/w3-total-cache/reviews/)

 * 9 replies
 * 2 participants
 * Last reply from: [Marko Vasiljevic](https://wordpress.org/support/users/vmarko/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/temp-disabling-all-caching-within-a-specific-function/#post-15235331)
 * Status: resolved