After importing content, mass-deleting posts, migrating a site, or restoring a database after a failure, the wp_term_relationships table may contain relationships to objects that no longer exist in wp_posts. These rows are known as orphaned taxonomy relationships.
They do not always cause an immediately visible error, but they can distort category and tag counts, complicate taxonomy processing, and produce incorrect results when plugins or custom queries access the data. Below is a safe process for checking, cleaning up, and recounting terms.
What Are Orphaned Relationships in WordPress?
WordPress stores category, tag, and custom taxonomy assignments in the wp_term_relationships table. It contains at least two important fields: object_id, the ID of the post or other object, and term_taxonomy_id, the ID of the term within a specific taxonomy.
A relationship is considered orphaned when its object_id no longer matches any post in wp_posts. For example, a post may have been deleted without properly cleaning up its related rows, or only part of the tables may have been restored from a backup.
The wp_posts table contains more than regular blog posts. It also stores pages, attachments, and posts belonging to custom post types. Therefore, a check against wp_posts covers all standard objects that can have taxonomy relationships.
- incorrect or outdated term counts;
- unnecessary database rows after an import or mass deletion;
- incorrect taxonomy archive behavior or problems with plugins that read relationships directly;
- slower taxonomy queries on large sites.
Preparation: Back Up the Database and Check the Table Prefix
Cleanup is performed directly in the database, so create a current dump before making any changes. The backup must be available for restoration, not merely successfully generated on the server.
The examples below use the wp_ prefix. On a specific site, it may be different, such as abc_ or site1_. Do not insert wp_ automatically: an incorrect prefix can cause an error or make you check the wrong table.
You can check the current prefix in the WordPress configuration or with WP-CLI if the command is available in your environment.
- run commands from the root directory of the relevant WordPress site;
- make sure WP-CLI connects to the correct database;
- for multisite installations, check which site and table set the operation applies to;
- inform your team or client about a short maintenance window if the site is actively changing data.
wp db export before-taxonomy-cleanup.sql
How to Find Orphaned Taxonomy Relationships
First, determine how many relationships point to posts that no longer exist. LEFT JOIN keeps rows from wp_term_relationships for which no matching post is found in wp_posts. The p.ID IS NULL condition selects precisely these cases.
Replace wp_ with the actual table prefix and run the query through WP-CLI.
wp db query "
SELECT COUNT(*) AS orphaned_relationships
FROM wp_term_relationships AS tr
LEFT JOIN wp_posts AS p ON p.ID = tr.object_id
WHERE p.ID IS NULL;
"
Additional Checks Before Deletion
If the count is not zero, do not delete the rows immediately. First, inspect a few examples and confirm that they really are relationships to nonexistent objects rather than the result of an incorrect table name or a connection to the wrong database.
wp db query "
SELECT tr.object_id, tr.term_taxonomy_id
FROM wp_term_relationships AS tr
LEFT JOIN wp_posts AS p ON p.ID = tr.object_id
WHERE p.ID IS NULL
LIMIT 50;
"
How to Safely Delete Orphaned Relationships
After verification, you can delete only those rows in wp_term_relationships for which no matching post exists in wp_posts. The query does not delete posts, terms, taxonomies, or media files—it removes only invalid relationships.
MySQL and MariaDB use DELETE with JOIN for this operation. Run it only after creating a backup and checking the table prefix again.
- do not broaden the condition without understanding the consequences;
- do not delete all rows for a specific taxonomy if the issue concerns only nonexistent object_id values;
- if the site is still importing content or performing mass deletions, stop those processes first so the data does not change during cleanup.
wp db query "
DELETE tr
FROM wp_term_relationships AS tr
LEFT JOIN wp_posts AS p ON p.ID = tr.object_id
WHERE p.ID IS NULL;
"
What to Do If the Command Returns an Error
The most common causes are an incorrect prefix, insufficient database-user permissions, or a different database management system. Check the connection with wp db cli or run the safe SELECT query first. Do not replace DELETE with an alternative mass cleanup of the table.
Recounting Categories, Tags, and Custom Taxonomies
After deleting orphaned relationships, recount the term totals. This synchronizes the stored count values with the actual number of related objects.
For standard taxonomies, use these WP-CLI commands:
wp term recount category
wp term recount post_tag
Custom Taxonomies
For a custom taxonomy, provide its system slug rather than the name shown to users in the admin area. For example, if the taxonomy is registered with the product_brand slug, the command is:
wp term recount product_brand
Verifying the Results After Cleanup
After DELETE, repeat the original SELECT query. Under normal circumstances, it should return zero. If orphaned relationships appear again, the issue may not be a one-time data problem but an import process, plugin, or custom code that creates relationships without checking whether the post exists.
Also check several category and tag archives, individual post pages, and search functionality. On sites with custom post types, review the relevant archives separately.
Make sure that no legitimate relationships were removed during cleanup. The query deletes only rows whose object_id is not present in wp_posts, so it should not affect existing posts or their taxonomy assignments.
- run the COUNT query again;
- check term counts in the relevant taxonomies;
- review archive pages and several posts with categories and tags;
- check importer logs if the issue appeared after an import.
wp db query "
SELECT COUNT(*) AS orphaned_relationships
FROM wp_term_relationships AS tr
LEFT JOIN wp_posts AS p ON p.ID = tr.object_id
WHERE p.ID IS NULL;
"
Common Mistakes When Cleaning Up Relationships
The most dangerous mistake is performing a mass deletion without a backup or using the wrong table prefix. Another common issue is confusing orphaned relationships with orphaned terms. This guide concerns missing posts in wp_posts. Checking terms that lack a corresponding row in wp_terms or wp_term_taxonomy is a separate type of diagnosis.
Do not manually delete rows from wp_posts or wp_terms to correct term counts. This can result in lost content or a damaged taxonomy structure. First determine what type of inconsistency exists in the database.
A nonzero result is not necessarily proof of a serious site error. Some plugins or nonstandard processes may temporarily change the data. The context in which the rows appeared and a follow-up check after cleanup are both important.
- do not use TRUNCATE on wp_term_relationships;
- do not edit the database while an import or migration is active;
- do not run the command against production if WP-CLI is connected to staging or another database;
- keep the dump until you have confirmed that the site is working correctly.
Frequently asked questions
Can orphaned relationships be deleted without a backup?
The query may technically run, but doing this without a backup is not recommended. An error in the prefix, database, or SQL condition could cause data loss, while a dump allows you to quickly restore the previous state.
Will this query delete the posts or categories themselves?
No. The query deletes only rows from wp_term_relationships that point to an object_id missing from wp_posts. It does not delete posts, terms, or taxonomies.
Why are category counts still incorrect after cleanup?
Run wp term recount for the relevant taxonomies. If the issue remains, check for other types of corrupted data, caching, plugin filters, or a process that is recreating invalid relationships.
Does this check include attachments and custom post types?
Yes. Attachments and custom post types are generally also stored in wp_posts, so the LEFT JOIN on object_id includes them. However, interpret the results in the context of the specific plugins' logic.
Conclusion
Cleaning up orphaned taxonomy relationships is a targeted operation that removes relationships to posts that no longer exist in the database. The safe process is to create a dump, verify the prefix, count orphaned rows, inspect examples, run a limited DELETE, and check the result again.
Afterward, recount terms with WP-CLI and review the site's archives. If invalid relationships reappear, investigate the import, migration, plugin, or custom code responsible instead of repeatedly running cleanup without analysis.