3355 | | // Check for duplicate slug |
3356 | | $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); |
3357 | | if ( $id && ($id != $term_id) ) { |
3358 | | // If an empty slug was passed or the parent changed, reset the slug to something unique. |
3359 | | // Otherwise, bail. |
3360 | | if ( $empty_slug || ( $parent != $term['parent']) ) |
3361 | | $slug = wp_unique_term_slug($slug, (object) $args); |
3362 | | else |
3363 | | return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); |
| 3355 | // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy. |
| 3356 | if ( $exists = term_exists( $slug, $taxonomy ) ) { |
| 3357 | $existing_term = get_term( $exists['term_id'], $taxonomy ); |
| 3358 | |
| 3359 | if ( $name === $existing_term->name ) { |
| 3360 | |
| 3361 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
| 3362 | $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) ); |
| 3363 | if ( in_array( $name, $siblings ) ) { |
| 3364 | return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists['term_id'] ); |
| 3365 | } |
| 3366 | |
| 3367 | } else { |
| 3368 | return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists['term_id'] ); |
| 3369 | } |
| 3370 | } |