0

I would like to change the permalink structure for new posts only. I want to leave the old ones as they are. I have a site that has over 30k posts and I'm worried that 301 redirects might mess up SEO. Is there any way to solve this in WordPress? Additionally, the solution should be compatible with Yoast SEO.

0

You can achieve that using a plugin like "Custom Permalinks"

The steps will be:

1- install and activate the plugin.

2- Save all your posts current permalinks into the custom meta field "custom_permalink" that meta field is the field that is used by the plugin to save the custom permalinks for each post and uses it as the permalink whatever the default structure is.

3- The permalink that you will save in "custom_permalink" field for each post is the relative permalink without the starting slash, e.g.:

if your post permalink is "https://yourdomain.com/something/something/", the part that will be saved in the meta field is "something/something/".

4- After saving permalinks of all your posts, you can now change the permalink structure, and the plugin will keep the old structure for your current posts because the structure is saved for each post as a custom permalink.

You can programmatically save the current permalink structure for all current posts before changing the structure will something like this:

add_action('admin_notices', function(){
    $posts = get_posts([
            'post_status' => 'publish',
            'post_type' => 'post',
            'no_found_rows' => true,
            'posts_per_page' => 100,
            'meta_query' => [
                    [
                            'key' => 'custom_permalink',
                            'compare' => 'NOT EXISTS'
                    ]
            ]
    ]);

    if(empty($posts)) {
        echo '<div class="notice notice-success"><p><strong>All custom permalinks were saved</strong></p></div>';
        return;
    }

    foreach ($posts as $post) {
        $customPermalink = str_replace(home_url('/'), '', get_permalink($post));
        update_post_meta($post->ID, 'custom_permalink', $customPermalink);
    }

});

The previous code will save the permalink for 100 posts with each dashboard page refresh until a completion message appears.

Important: You must remove this code when done using it.

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.