WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Stupidly, I truncated my posts table with out thinking about the term relationships. Now I have a bunch of 404s because there are broken term relationships. Is is possible to rebuild the table with the correct pairings?

share|improve this question
Your question is a little bit confusing. If you truncated your posts table what is there to rebuild? Why can't you just truncate your wp_term_relationships table? – totels Nov 15 '12 at 22:35
@totels I truncated the wp_posts table but I didn't truncate wp_term_relationships. Its looking like I'm going to have to rebuild the entire blog – user1491929 Nov 15 '12 at 23:13
Are you saying that you truncated wp_posts long ago and have already started entering new posts? – totels Nov 15 '12 at 23:24
@totels yes exactly – user1491929 Nov 16 '12 at 0:51
up vote 0 down vote accepted

If your AUTO_INCREMENT value wasn't reset and your new posts will still have unique IDs and you can do:

SELECT tr.*, p.ID
FROM wp_term_relationships AS tr
    LEFT JOIN wp_posts AS p ON tr.object_id = p.ID

to see which term relationships are orphans and use a DELETE FROM to fix it.

share|improve this answer
Or if you prefer a nested select: SELECT * FROM wp_term_relationships WHERE object_id NOT IN (SELECT ID FROM wp_posts); – totels Nov 16 '12 at 1:18
thank you for your attempt. unfortunatly it returned 0 results. It apears that my auto_increment was indeed reset. – user1491929 Nov 16 '12 at 1:21
Even if your auto_increment was reset it is unlikely you would get 0 results unless you now have more posts than you did before you truncated. What is the output of the Auto_Increment field when you run SHOW TABLE STATUS LIKE 'wp_posts';? As compared to SELECT MAX(ID) FROM wp_posts;? – totels Nov 16 '12 at 1:30
i dumped the wp_term_relationships table and rebuild the tags. Luckily the tagging was generated by a script i wrote so it was easy to re tag my posts. Thanks for your help. – user1491929 Nov 16 '12 at 2:38
updated with solution below – user1491929 Nov 16 '12 at 17:03

warning: Truncating your wp_term_relationships deletes everything in that table! if you want to only delete tag term relationships then make sure you use the correct sql.

After truncating the wp_term_relationships table I used a custom script to re-tag my posts. You can do something similar like this:

$numposts = wp_count_posts();
$posts = get_posts(array('numberposts'=>(int)$numposts->publish));
foreach($posts as $post){
    $tags = myCustomTaggingFunc($post); //this returns an array of tags

pretty simple.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.