Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to edit/remove a term that's in multiple taxonomies? WordPress

Some of the terms on my site appear with the same exact slug, in multiple taxonomies.

For example: 'education' appears in both the 'department' taxonomy and the 'topic' taxonomy.

Now, I can't seem to modify or delete a term from one taxonomy without affecting the term in the other taxonomy.

If I change 'education' to 'department-of-education' in the 'department' taxonomy, it changes to 'department-of-education' in the 'topic' taxonomy as well.

If I delete 'education' from departments, I am guessing it will disappear from topics as well.

How can I separate the same-named terms? A "manual" solution using bulk edit functionality to retag is not ideal because of the number of posts and duplicate terms I have (bulk-adding terms to too many entries in the WP admin causes a Request URI too large error.)

Wordpress does seem to prevent same-named terms in different taxonomies in the admin, but my taxonomies and terms were imported in the course of a site migration using a WXR file, and Wordpress let them in—so now I'm stuck with them.

I tried posting this on WPSE, without success.

Note/elaboration: What I need to do at the moment is delete about 20 terms from taxonomy1 that are also in taxonomy2, without losing those terms in taxonomy2.

Answers (4)

2011-09-18

Milan Petrovic answers:

I had the same problem, and I wasn't able to find the solution. But, I think that there is a bug in WordPress that sometimes allow for this to happen if one of the taxonomies is category and the other is custom taxonomy.


web559 comments:

These are both custom taxonomies, and they were let in via WXR imports containing bits like this—

<category domain="subject" nicename="education"><![CDATA[Education]]></category>
<category domain="department" nicename="education"><![CDATA[Education]]></category>


Milan Petrovic comments:

Most likely import procedure doesn't check for the terms and taxonomies. But, the problem exists in the admin taxonomy terms pages also, not only import. I will be looking to WordPress development TRAC for some answers also.

2011-09-19

Julio Potier answers:

Hello, did you try the "Simple tags" plugin ? See you


web559 comments:

I have seen it but don't know how it would solve this issue. What aspect of Simple Tags are you thinking of?


Julio Potier comments:

Sorry, i thought about "tags" and not "terms", am i wrong ?


web559 comments:

These are both custom taxonomies.

2011-09-19

Maor Barazany answers:

That seems to be [[LINK href="http://core.trac.wordpress.org/ticket/12911"]]a known issue[[/LINK]], that there are problems having sames slugs for different terms in different taxonomies.
I don't think it fixed yet. Anyway, I think that the best way is
1. Backup your db to avoid and data loss with this.
2. Rename the slugs - and not the term name - of the duplicate slugs, and then only delete.
3. If that is not helpful, you will have to manually change the slug directly in the database, checking by term_id the appropriate terms you change.


web559 comments:

The duplicate terms have the same ID, so changing the slug changes the instance in both taxonomies. So changing the slug in the front end or the database won't resolve it, unfortunately.


Maor Barazany comments:

How have you reached the situation to have 2 terms with 1 term_id?
If so, I believe the only way to do it is editing directly in the database.
If you are familiar with the database and not afraid changing directly there, you may do some things.
<strong>Important! </strong> Make a full backup mysqldump to your database, so you will be safe if something will go wrong. Also, if you can, it is better doing it on a development offline copy of the site, and not on production. After testing the solution and if it works, you can apply it to your production database (of course, backing it up also).

I have not tested the solution, but this is what I would have tried to do. I believe it should solve the issue.
I assume you use a GUI interface to the database (such as phpmyadmin, WorkBench, HeidiSQL etc.), and not with CLI.

1. First, go to <strong>wp_terms</strong> table and find your "problemtic" term -
You can use this SQL statement to find it-
SELECT * FROM `wp_terms` WHERE name = 'tagname';
Or this one -
SELECT * FROM `wp_terms` WHERE name like '%tagname%';

Write the term_id you need.

2. Go to <strong>wp_term_taxonomy</strong> table, and the locate all the two taxonomies that this term is now belonged to.
You can use this SQL statement to find it, lets say the id of the term was 65 -

SELECT * FROM `wp_term_taxonomy` WHERE term_id = '65';

You will probably find here 2 rows - one for each taxonomy.
You should delete the row that has the taxonomy where you don't want the term for it.
Before you delete the row - write down the term_taxonomy_id in the row of the taxonomy that will be deleted and do the 3rd step

3. Go to the <strong>wp_term_relashionships</strong> and locate all the posts that are currently attached to that term, that in the taxonomy that will be removed.
You can use this SQL statement to find it, lets say the term_taxonomy_id was 100 -
SELECT * FROM `wp_term_relationships` WHERE term_taxonomy_id = '100';
Delete all the rows you find.


4. Now we need to remove the term from being attached to the taxonomy. Using the same term_taxonomy_id = '100', run this -
SELECT * FROM `wp_term_taxonomy` WHERE term_taxonomy_id = '100';
Delete the row it finds.


Now it should be fine.
I advise that you will first create a new term instead the one you are going to delete, and attached the relevant posts, so you won't miss any post outside the term, after deleting the duplicate one from the two.

<strong>Do everything really carefully and slowly step by step</strong>
I hope it helped.


Maor Barazany comments:

I reproduced your issue now in a local dev site. Yes - if you create a term in the same name in 2 different taxonomies - the term will have the same id.
(This is why when you you term functions, you always need to tell WP also from which taxonomy to pull it).

I tested my solution - and it worked for me in my dev env.


web559 comments:

Thanks for all your testing. Could you explain what the difference is between term_taxonomy_id just and term_id? In my wp_terms_taxonomy table, they are sometimes the same number and sometimes not.


Maor Barazany comments:

term_taxonomy_id is the id of the "connection" between a term and a taxonomy.
term_id is id of the term itself.
They may be sometimes the same numbers, but it does not necessarily tells something.

Have you tried my solution suggestion?

2011-09-19

Abdessamad Idrissi answers:

Well this is somthing WordPress suffered from since 4 years! and it is tagged as a <em>wontfix </em>issue.

____________
First of all, please backup you database! and if possible; try this first in your development site on localhost before applying it to the online production site.
____________

Does those duplicate terms have the same tag_ID? then, the solution is not deleting them but just un-parent them from taxonomy1 so they will belong only to taxonomy2.

If they have different ID's but same slugs try puting this code in your "functions.php"

function clean_old_taxonomies(){
$terms = array(1, 2, 3, 4, 5, 6, 20); // change this to the term id's you want to delete
$taxonomy = "taxonomy1 "; // taxonomy to delete from

$term_children = array();

foreach ($terms as $term) {
$children = get_term_children( $term, $taxonomy );
if ( !empty($children) )
$term_children[] = $children;
}

foreach ($term_children as $child) {
wp_delete_term( $child[0], $taxonomy );
}
}
add_action( 'admin_init', 'clean_old_taxonomies');

then visit your admin dashboard to execute the above script.

hope it works :)


web559 comments:

The terms do have the same ID. Can you explain how to un-parent them from a taxonomy? Is there a programmatic way to do this other than term by term in the database?

The attached image shows how the duplicates are showing up in the term_taxonomy table. I'm not really sure what to make of it—specifically the difference between term_taxonomy_id and term_id.