0

Is there a way to bulk add one word at the end of each URL for posts? I have around 3500 posts and it would be time consuming to add to each one.

For example URL's are

www.example.com/my-post/
www.examle.com/new-post/

And I want to add one word to end of URL

www.example.com/my-post-word/
www.examle.com/new-post-word/

Is there MySql script to do such thing?

  • This could be done with multiple ways. 1) Wordpress Loop 2) SQL like UPDATE wp_posts SET post_name = CONCAT(post_name,'-word') where post_type='post' You can add ADD more types for filtering. BUT TAKE A BACKUP FIRST!!!!^inf – Drupalizeme Nov 21 '17 at 13:18
  • This solution also worked :)! – David Nov 28 '17 at 0:08
1

Here is your code:

if ( isset($_GET['slug-update']) ) {

    $posts = get_posts();

    foreach($posts as $singlepost) {
        if($singlepost->post_status === 'publish'){
            $post_id = $singlepost->ID;
            $current_slug = $singlepost->post_name;
            $updated_slug = $current_slug . '-word';
        }

        wp_update_post( array(
            'ID' => $post_id,
            'post_name' => $updated_slug ) );
    }

    die('Slug Updated');    
}

Add this to your theme's function.php. Then run this with www.example.com/?slug-update

P.S. this will only update the default post type 'post' (backup the database to be secure)

Let me know if this helped.

  • I'd recommend putting this into a wpcli command instead of the functions.php. – Welcher Nov 21 '17 at 13:46
0

Can't you just add it inside the WP admin permalink settings? enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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