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.

is it possible to regenerate the slugs programmatically after changing the titles of the post?
The girl that updated the title of more than 200 posts just "forgot" to update the slug too, so i was wondering if there was some sort of plugin that regenerates the slugs from the new titles

share|improve this question
    
I have had to do this several times and found that between different server environments where it can't handle large arrays ( with numberposts being set to unlimited ) nor calling wp_update_post repeatedly with large memory consumption that breaking it into a WP_Query call with pagination and using $wpdb that it is more manageable and performant. I provided the code sample on a similar post. –  codearachnid Feb 27 at 3:14

2 Answers 2

up vote 4 down vote accepted

Yes, it is possible.

Sample code, has to be tested and refined:

// get all posts
$posts = get_posts( array (  'numberposts' => -1 ) );

foreach ( $posts as $post )
{
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

I just made this up, there are probably some errors and egde cases, but it should give you an idea. Also, this may take a while, so it could be useful to split the update into smaller chunks.

share|improve this answer

This plugin also does the job: http://www.jerrytravis.com/598/wordpress-plugin-to-generate-post-slugs

However, as it only does it for posts which don't have a slug yet, if you need to regenerate slugs edit the following line in the plugin:

if ($post->post_name == "") {

for example, you could change it to:

if (true) {

share|improve this answer

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.