Skip to content

Instantly share code, notes, and snippets.

@meetawahab
Last active March 13, 2020 06:32
Show Gist options
  • Save meetawahab/13d05e60a36b0b742a54f8df32673c6a to your computer and use it in GitHub Desktop.
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
<?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;
}
@meetawahab
Copy link
Author

meetawahab commented Mar 13, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment