I have posts URL like https://www.website.com/%post_name%/ and want to redirect all to new permalink structure which is https://www.website.com/%category%/news/%post_name%/
I've tried same structure with redirection plugin it did not work. I think it can be solved via custom coding with wordpress pre-built functions which i've never use them.
add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
global $post;
// this array can contain category names, slugs or even IDs.
$catArray = ['premier-lig'];
if (is_single($post->ID) && has_category($catArray, $post)) {
$new_url = "https://www.website.com/{$post->post_category}/news/{$post-> $post_name}";
wp_redirect($new_url, 301);
exit;
}
}
When i go to the link https://www.website.com/postname it told me this url redirected too much. I only want https://www.website.com/postname redirected to https://www.website.com/postcategory/news/postname