WordPress Duplicate term_id resulting in MultiSite Errors

Issue

We have some users that have added the same tag and category to a blog. When they do this, they are unable to edit or use those tags / categories and receive an error basically stating “You need a higher level of permission.”

So far i’ve determined the actual error being thrown is Term ID is shared between multiple taxonomies error that we have been receiving when trying to edit / delete certain post categories or post tags.

Debugging this further, the issue seems to be happening at creation time. When I look at the tables in the database the terms table looks fine, but the term_taxonomy table does not. The same term_id is being saved for both entries.

MariaDB [wordpress]> select * from wp_62_terms;
+---------+-----------------------+-----------------------+------------+
| term_id | name                  | slug                  | term_group |
+---------+-----------------------+-----------------------+------------+
|       1 | Uncategorized         | uncategorized         |          0 |
|       2 | Blogroll              | blogroll              |          0 |
|  107691 | ppppp                 | ppppp                 |          0 |
|  107692 | ppppp                 | ppppp                 |          0 |
+---------+-----------------------+-----------------------+------------+

MariaDB [wordpress]> select * from wp_62_term_taxonomy;
+------------------+---------+---------------+-------------+--------+-------+
| term_taxonomy_id | term_id | taxonomy      | description | parent | count |
+------------------+---------+---------------+-------------+--------+-------+
|                1 |       1 | category      |             |      0 |    19 |
|                2 |       2 | link_category |             |      0 |     0 |
|               34 |  107691 | post_tag      |             |      0 |     0 |
|               35 |  107691 | category      |             |      0 |     0 |
+------------------+---------+---------------+-------------+--------+-------+

I have been debugging this further and captured the $wpdb->last_query value for that able insert and those read as follows:

INSERT INTO `wp_62_term_taxonomy` (`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (107691, 'post_tag', '', 0, 0)

INSERT INTO `wp_62_term_taxonomy` (`term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (107692, 'category', '', 0, 0)

The INSERT sql shows the correct term_id however – that is not what is getting stored in the database.

Manually updating the database value does correct the problem.

Any thoughts / ideas are appreciated!

Solution

This issue was caused by what seems to be a legacy and a potentially unused feature of WordPress Multisite. When the global_terms_enabled value is enabled in the wp_sitemeta table, it will create a global term list in the wp_sitecategories table.

Within the wp-includes\ms-functions.php file, the global_terms function is called when a new taxonomy term is added. Within that function, it does a check in the wp_sitecategories table to see if that term exists. However – that logic in that function is flawed and does not account for the same value across multiple taxonomies. It detects it as a duplicate, and then issues wpdb queries to change the term_id values as noted above.

This was logged as a bug in the WordPress Core here: https://core.trac.wordpress.org/ticket/55979

Changing the sitemeta value of global_terms_enabled from 1 to 0 solved this behavior.

Answered By – Aaron A

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published