Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I just imported 1000 post into a custom post type on a site and the slugs are all blank. There is a problem with the importing plugin i'm using. My SQL is weak :-/ know what i need to do but not sure who to write the query.

I need help writing a query to run in phpmyadmin to copy post_title to post_name where post_type is people.

Thanks for the help

share|improve this question
    
This is a pure MySQL question, off-topic here, and probably with solutions posted in Database Administrators or Stack Overflow. –  brasofilo Apr 22 '13 at 1:10
2  
You cannot just copy the title to the name field. Both follow different rules: name is for the URL. –  toscho Apr 22 '13 at 8:01
add comment

2 Answers

You do not want a simple SQL solution. post_name is used to generate permalinks. If you simply copy the title to the name (slug) you will have spaces and punctuation in the URL, and that is going to be a problem. It will result in broken links. Additionally, you could end up with two posts having the same permalink which will result in unpredictable behavior.

With SQL it should be possible to replace the spaces with dashes, and strip the punctuation, as WordPress does, but resolving the duplicate permalinks issue would be difficult in pure SQL. Let WordPress fix it. Place the following in functions.php and remove it after you fix the issue.

function fix_post_slugs() {
  $posts = new WP_Query(array('post_type'=>'book'));
  if (empty($posts->posts)) return false;
  foreach ($posts->posts as $p) {
    wp_update_post((array)$p);
  }
}
fix_post_slugs();

What you are doing is grabbing a post object and passing it back to wp_update_post as an array. All of the checks and balances run that would ordinarily sort out things like the slug. Of course, you will have to adjust the post type name to suit your install.

Tested, but barely. I would run this on a dummy database before trusting it with real data. I am fairly sure that this will do no damage, but paranoia is a virtue. Make backups first.

share|improve this answer
add comment

I was afraid of trying the function and found the answer from a developer friend. I used it and it worked. Thanks

update wp_posts set post_name = post_title where post_type='children'
share|improve this answer
1  
I can only repeat @toschos comment: "You cannot just copy the title to the name field. Both follow different rules: name is for the URL." Didn't you read/understand that? Edit: if you only use this once (where you have a one-word alphabetical-only name) this is okay, though. –  t f Apr 22 '13 at 14:38
1  
To emphasize tosco's comment and tf's. This likely won't work. While the SQL is technically correct, your friend doesn't understand the purpose of this field. post_name is used to generate permalinks. If you do this you will have spaces in the URL, and that is going to be a problem. It will result in broken links. Additionally, you could end up with two posts having the same permalink which will result in unpredictable behavior. There is a reason (several) I did not suggest a simple SQL solution. –  s_ha_dum Apr 22 '13 at 15:32
    
Sorry I should have mentioned that my titles for that post type are all unique numbers. –  sebastienb Apr 23 '13 at 14:40
add comment

Your Answer

 
discard

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.