Send Single Entries of a Post Type to Its Archive
Redirects the single pages of post types that only make sense in a list, such as testimonials, FAQs or team members, to their archive or another page.
Opens WPIDE on
Code
// Post types whose single pages redirect.
$wpide_list_only_types = array( 'testimonial' );
// Optional fixed target path, such as '/reviews/'. Leave empty to use the archive.
$wpide_list_only_target = '';
add_action( 'template_redirect', function () use ( $wpide_list_only_types, $wpide_list_only_target ) {
if ( ! is_singular( $wpide_list_only_types ) ) {
return;
}
if ( '' !== $wpide_list_only_target ) {
$target = home_url( $wpide_list_only_target );
} else {
$archive = get_post_type_archive_link( get_post_type() );
$target = $archive ? $archive : home_url( '/' );
}
wp_safe_redirect( $target, 301 );
exit;
} );
Notes
Set the post types and, optionally, a fixed target path. Without a target, the post type archive (or the homepage) is used.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.