Skip to content

Instantly share code, notes, and snippets.

@alexwcoleman
Created September 9, 2017 18:07
Show Gist options
  • Save alexwcoleman/cc08ff4b4a19b5eb4f42674824cd458f to your computer and use it in GitHub Desktop.
Save alexwcoleman/cc08ff4b4a19b5eb4f42674824cd458f to your computer and use it in GitHub Desktop.
WordPress: Redirect single custom post type to archive page.
// redirect single posts to the archive page, scrolled to the current member.
// * this is for a post type of "board_member"
add_action( 'template_redirect', function() {
if ( is_singular('board_member') ) {
// I used the two variables to put the member's name as the ID of the article
// so the redirect would automatically scroll. This can be removed.
$title = get_the_title();
$url_append = str_replace( ' ', '-', strtolower($title) );
global $post;
$redirectLink = get_post_type_archive_link( 'board_member' ) . "#" . $url_append;
wp_redirect( $redirectLink, 302 );
exit;
}
});
@kaoskeya
Copy link

kaoskeya commented Dec 8, 2017

You could use the built in sanitize_title in line 8.

Use $url_append = sanitize_title($title); instead of $url_append = str_replace( ' ', '-', strtolower($title) );

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