Last active
March 13, 2020 06:32
-
-
Save meetawahab/13d05e60a36b0b742a54f8df32673c6a to your computer and use it in GitHub Desktop.
Redirect all Single Posts of a Custom Post Type using template_redirect Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'template_redirect', 'redirect_your_post_type_single' ); | |
function redirect_your_post_type_single(){ | |
if ( ! is_singular( 'YOUR-CUSTOM-POST-TYPE' ) ) | |
return; | |
wp_redirect( get_page_link( YOUR-PAGE-ID ), 301 ); | |
// or | |
// wp_redirect( get_post_type_archive_link( 'ANOTHER-CUSTOM-POST-TYPE' ), 301 ); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Items to Note:
Change ‘YOUR-CUSTOM-POST-TYPE’ to the name of the Custom Post Type you would like to have redirected.
Change ‘YOUR-PAGE-ID’ to the ID of the Page you would like your Custom Post Type’s Single Posts to redirect to.
Or Change ‘ANOTHER-CUSTOM-POST-TYPE’ to the name of the Custom Post Type of the Page you would like your Custom Post Type’s Single Posts to redirect to.
If you followed the instructions correctly all of the Single Posts of your Custom Post Type will now be redirected to your desired location.