Title: Problem When Using Object Caching
Last modified: September 1, 2016

---

# Problem When Using Object Caching

 *  [julien731](https://wordpress.org/support/users/julien731/)
 * (@julien731)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/problem-when-using-object-caching/)
 * I have been running into an issue that occurs when CPT-onomies is used on a site
   that has object caching enabled. This is a pretty big issue as it basically breaks
   the plugin.
 * Also, I should add that the problem occurs when terms are requested more than
   once per runtime.
 * Here is what happens with object caching and multiple requests for CPT-onomies:
    - Terms queried via `get_the_terms()`
    - It is the first run so the object cache is empty. `wp_get_object_terms()` 
      is ran
    - `wp_get_object_terms()` queries the database and doesn’t find anything, populates
      object cache with blanks/errors
    - CPT-onomies hooks at the end of `wp_get_object_terms()` and does its own querying,
      returns “correct” results
    - `get_the_terms()` returns correct results
 * On the first run, all is fine. However, if a second run is triggered:
    - `get_the_terms()` checks object cache (relationships) which isn’t empty anymore
    - Runs `get_term()` on each term. Because the terms don’t actually exist in 
      the database, the function returns a _Invalid Term_ error
    - Returns the error, never runs `wp_get_object_terms()` and thus never runs 
      the CPT-onomies filters
 * It is basically impossible to use the plugin in this particular scenario.
 * [https://wordpress.org/plugins/cpt-onomies/](https://wordpress.org/plugins/cpt-onomies/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [julien731](https://wordpress.org/support/users/julien731/)
 * (@julien731)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/problem-when-using-object-caching/#post-7682744)
 * Because I am currently working on a project that has to go live very soon, I 
   have written a (dirty) workaround that basically bypasses the object cache. It
   is not good, but for now it works. I will be looking for a real solution later
   on.
 *     ```
       add_filter( 'get_the_terms', 'cpt_onomies_object_cache_workaround', 10, 3 );
       /**
        * Clear CPT-onomies terms relationships object cache
        *
        * There is a bug in CPT-onomies that breaks the plugin when get_the_terms() is ran more than once and object caching
        * is enabled. I have reported the bug on the support forum
        * (https://wordpress.org/support/topic/problem-when-using-object-caching?replies=1#post-8775025).
        *
        * Because the site needs to go live fast, this workaround will be used for now. It is definitely dirty and should be
        * removed when a real fix comes up.
        *
        * @param  array|WP_Error $terms    List of attached terms, or WP_Error on failure.
        * @param  int            $post_id  Post ID
        * @param  string         $taxonomy The taxonomy name
        *
        * @return array|WP_Error           Un-altered terms
        */
       function cpt_onomies_object_cache_workaround( $terms, $post_id, $taxonomy ) {
   
       	// Only delete the cache for my custom CPT-onomies
       	if ( in_array( $taxonomy, array( 'my_cptonomies' ) ) ) {
       		wp_cache_delete( $post_id, $taxonomy . '_relationships' );
       	}
   
       	return $terms;
   
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Problem When Using Object Caching’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/cpt-onomies_f2f2f2.svg)
 * [CPT-onomies: Using Custom Post Types as Taxonomies](https://wordpress.org/plugins/cpt-onomies/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cpt-onomies/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cpt-onomies/)
 * [Active Topics](https://wordpress.org/support/plugin/cpt-onomies/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cpt-onomies/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cpt-onomies/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [julien731](https://wordpress.org/support/users/julien731/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/problem-when-using-object-caching/#post-7682744)
 * Status: not resolved