0

I have this permalink structure in wordpress /%category%/%postname%/

now suppose i have written a article article with title "Hello World" and i selected two categories for this article "cat1" and "cat2" now "cat1" is my primary category and i selected this using RankMath Plugin

just because "cat1" is my primary category for that post the url become

https://example.com/cat1/hello-world/ when we go to this the post "Hello World" is loaded.

but when I manually go to the same post with "cat2" https://example.com/cat2/hello-world/ the same article "Hello World" is loaded.

Now https://example.com/cat1/hello-world/ & https://example.com/cat2/hello-world/ both urls are working individually and this is a problem

this url with secondary category https://example.com/cat2/hello-world/ should get 404 or automaticllay redirect to primary category url https://example.com/cat1/hello-world/

but the problem is if same post is working with "cat2" also

this way i have two url for same article, how to fix this? Please Help.

1 Answer 1

0
function get_current_post_ID() {
    $postid = get_queried_object_id();
    $post_url = get_permalink( $postid );
    //wp_redirect( $post_url );
    if( has_category('', $postid) && urldecode('https://'.$_SERVER ['HTTP_HOST'].$_SERVER ['REQUEST_URI']) != $post_url ) :
        wp_redirect( $post_url, 308 );
        exit();
    endif;
}
add_action( 'template_redirect', 'get_current_post_ID' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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