Support » Fixing WordPress » How best to fix accidentally deleted post slugs

  • Resolved Hedley


    My situation is that some, but not all, posts (possibly ~600-800) of a client site have somehow had the slug stripped out of the post_name field of the wp_posts table. Hence, the URL for these posts defaults to the site root (ie: “”).

    I assume that this was the result of some admin user error, possibly when doing a DB search/replace. The question is, how to fix it. It appears that it’s been this way for a while, as the backups available to me also have this problem.

    I assume that the best way would be to use a phpmyadmin mysql query to build new slugs for the affected posts using the content of post_title as the source material (stripping spaces and punctuation), but I’m not a skilled mysql query writer. I can successfully search for the affected posts:

    SELECT *
    FROM wp_posts
    WHERE post_name = '' AND post_status = 'publish'; 

    But beyond that, I’m guessing. Obviously I need to do a crash course on sql queries, but in the short term, does anyone have any tips, links to similar problem/resolutions, or alternate resolution suggestions?



Viewing 4 replies - 1 through 4 (of 4 total)
  • Keith


    Obviously I need to do a crash course on sql queries

    …try a plugin:

    As far as I know those plugins don’t allow one to search specific fields within tables for strings, and don’t allow one to replace those strings with post-specific content from another field in the same table (never mind that they lack the ability to reformat the post_title so that it contains no spaces or punctuation before it replaces the post_name).

    I find that if I open up an affected post and click “update”, the slug gets re-created, thereby solving the problem, on that post (I’d only have to do that 600-800 times). Were there some batch tool to force posts to be re-saved, that would also be an alternate solution, but I suspect that that is the stuff of fantasy.

    It eventually occurred to me that I could use the bulk editor to open twenty posts at a time, and then save them (with no actual changes to the posts), which re-created the slugs. I did this 40 times, and it’s all good again. Not as interesting as a sql query, but effective.

    • This reply was modified 1 year, 5 months ago by  Hedley.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How best to fix accidentally deleted post slugs’ is closed to new replies.