[wp-trac] [WordPress Trac] #39984: wp_insert_term can return wrong ID when multiple existing terms are found with the same name but differnet parent

WordPress Trac noreply at wordpress.org
Tue Feb 28 13:37:26 UTC 2017


#39984: wp_insert_term can return wrong ID when multiple existing terms are found
with the same name but differnet parent
--------------------------+-----------------------------
 Reporter:  mikejolley    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 First reported here as an API issue
 https://github.com/woocommerce/woocommerce/issues/13386, it seems the
 problem is within the {{{wp_insert_term}}} logic.

 '''To reproduce:'''

 Create 2 terms with the same name, but different parents e.g.

 - Top Level 1
  - Child
 - Top Level 2
  - Child

 You'll have 2 terms with name `Child` and slugs `child` and `child-top-
 level-2`.

 Now try to add the same named `Child` term again to the second top level
 parent (in my case, `Top Level 2`).

 {{{
 var_dump( wp_insert_term( 'Child', 'category', array( 'parent' => 1158 ) )
 );
 }}}

 The error message wil contain the term ID underneath the first top level
 parent (1159) instead of the correct one term under the chosen parent
 (1158):

 {{{
 object(WP_Error)#1270 (2) {
   ["errors"]=>
   array(1) {
     ["term_exists"]=>
     array(1) {
       [0]=>
       string(62) "A term with the name provided already exists with this
 parent."
     }
   }
   ["error_data"]=>
   array(1) {
     ["term_exists"]=>
     int(1159)
   }
 }
 }}}

 I've traced this back to the `$name_matches` code inside the
 `wp_insert_term` function. This code does not use the parent right now.

 Patch to follow.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39984>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list