Skip to content

Instantly share code, notes, and snippets.

@robertpassaro
Created July 9, 2016 18:06
Show Gist options
  • Save robertpassaro/2401dede5f1e003e7a0daa6993aa7c99 to your computer and use it in GitHub Desktop.
Save robertpassaro/2401dede5f1e003e7a0daa6993aa7c99 to your computer and use it in GitHub Desktop.
/*
* Keep WordPress native custom-post-type archive view, while redirecting any single views back to the archive.
* To use when single views are not wanted or needed.
* Takes no parameters.
*/
add_action( 'template_redirect', 'fq_disable_single_cpt_views' );
function fq_disable_single_cpt_views() {
$cpts_without_single_views = array( 'xxxx_my-cpt_xxx', 'xxxx_another-cpt_xxx' );
$queried_post_type = get_query_var('post_type');
if ( is_single() && in_array( $queried_post_type, $cpts_without_single_views ) ) {
wp_redirect( home_url( '/' . $queried_post_type . '/' ), 301 );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment