Skip to content

Instantly share code, notes, and snippets.

@wujekbogdan
Last active September 16, 2015 16:41
Show Gist options
  • Save wujekbogdan/d555f6c7a336605c3f3c to your computer and use it in GitHub Desktop.
Save wujekbogdan/d555f6c7a336605c3f3c to your computer and use it in GitHub Desktop.
Wordpress: disables singulars for custom post types
/**
* Disables singular post view for custom post types. Throws 404 error instead.
* @param $query
*/
function my_prefix_disable_singulars()
{
global $wp_query;
$disabled_singulars = array('post-type-1', 'post-type-2', 'post-type-3');
if (($wp_query->is_main_query() && $wp_query->is_singular && !empty($wp_query->query['post_type']) && in_array($wp_query->query['post_type'], $disabled_singulars))) {
$wp_query->set_404();
status_header(404);
require __DIR__ . '/path/to/404/template.php';
exit();
}
}
add_action('wp', 'my_prefix_disable_singulars');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment