WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#18628 closed defect (bug) (duplicate)

wp_insert_term bug doesn't update edit-tags.php right side tree

Reported by: justindgivens Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.2.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

With a custom taxonomy if I use wp_insert_term() and insert a couple terms then process to the wp-admin/edit-tags.php page for that term the right side "tree" don't display the correct "tree". It might display one or two new items, but not all. Here some test code to duplicate the bug.

<?php
$category = array('Built-In Oven','Double','Electric'); //Nested Hierarchy Terms to be inserted.
// Term Hierarchy should come out like 'Built-In Oven' -> 'Double' -> 'Electric'

$catCount = 0;
$tempCatArray = array();
foreach($category as $cat) {
  if ($catCount == 0) {
    $term_id = term_exists( $cat , 'product_type' );
    if ($term_id == 0) {
      $term_id = wp_insert_term( $cat , 'product_type' ); //This term shows up but no children do.
      array_push( $tempCatArray , $term_id['term_id'] );
    } else {
      array_push( $tempCatArray , $term_id['term_id'] );
    }
  } else {
    $term_id = term_exists( $cat , 'product_type' , (int) $tempCatArray[$catCount-1] );
    if ($term_id == 0) {
      $term_id = wp_insert_term( $cat , 'product_type' , array( 'parent' => (int) $tempCatArray[$catCount-1] ) ); //New inserts aren't showing until I resaved a term.
      array_push( $tempCatArray , $term_id['term_id'] );
    } else {
      array_push( $tempCatArray , $term_id['term_id'] );
    }
  }
  $catCount++;
}

?>

Now if I go and edit a term with no changes and hit save. The tree is now updated correctly. I don't even have to edit one of the new terms, just any. I've attached two images to show this happening.

Attachments (2)

before.png (38.8 KB) - added by justindgivens 10 years ago.
before editing a term and saving it.
after.png (38.7 KB) - added by justindgivens 10 years ago.
After saving a term the tree is now updated.

Download all attachments as: .zip

Change History (6)

@justindgivens
10 years ago

before editing a term and saving it.

@justindgivens
10 years ago

After saving a term the tree is now updated.

#2 @duck_
10 years ago

The problem you're facing is caching. wp_insert_term() will call functions to clear out the cache that stops the hierarchy displaying properly (clean_term_cache() -> _get_term_hierarchy()). So if you were to only add one term all would be okay. However, you're inserting multiple terms on a single page load...

  • clean_term_cache() contains some code, in the form of a static variable, to stop a taxonomy's hierarchy being rebuilt multiple times on a single page load.
  • because the code executes so fast the last_changed cache item in the terms group will not actually be changed on every term insert. Therefore, even if the above didn't occur, the result of get_terms() would be cached and only include the first term to be inserted (and any which existing prior) so _get_term_hierarchy() will return 'incorrect' information.

I'm afraid I cannot think of a nice workaround right now.

#3 @justindgivens
10 years ago

With the script above, could I not run

do_action('clean_term_cache', $tempCatArray, 'product_type');

after the foreach finishes?

#4 @scribu
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

@justindgivens: Calling the 'clean_term_cache' won't do much.

Going to close this as a dup of #14485 since it's essentially the same issue.

Note: See TracTickets for help on using tickets.