rhubarbgroup / .github

Community Health Files
https://github.com/rhubarbgroup/
0 stars 0 forks source link

term_exists not detecting recently created terms while object cache is active #22

Closed seldimi closed 1 year ago

seldimi commented 1 year ago

Hello Since WordPress 6.0, term_exists is using get_terms and caches functionality. Let's suppose we a foreach a loop where we need to check or create a taxonomy term on a loop. This will create 5 similar terms with name "test term". If you re-run the code, term_exists is running ok, so no more duplicates. If you delete all terms and modify for loop to 10, and run, it will create 10 instances of "test term"

That was not happening prior to 5.9 WordPress.

for ($i=0;$i<5;$i++) {
    create_term("test term","product_cat",[]);
}

function create_term($name,$taxonomy,$args  ) {
        $term = term_exists( $taxonomy_term, $taxonomy, $args['parent'] ?? 0 );

        if (0 !== $term && null !== $term) {
            $term_id = $term['term_id'];
        } else {
            $term = wp_insert_term($taxonomy_term, $taxonomy,$args);
            if (!is_wp_error($term)) {
                $term_id = $term['term_id'];
            } else {
                $term_id = null;
            }
        }
    }

Any ideas on how to fix that?

seldimi commented 1 year ago

To help up a bit, tried both solutions here but non worked. https://make.wordpress.org/core/2022/04/28/taxonomy-performance-improvements-in-wordpress-6-0/

tillkruss commented 1 year ago