I have a custom theme with a bunch of custom post types. I want to keep my "news" post type to display as a paginated list (that links directly to the external news article - already figured this part out), but I want to get rid of all single pages that get created when I add a news news post. Basically the opposite of this: How do I create new content pages for my Custom Post Type?. I apologize if this as too vague, not a developer. Just let me know what additional info is needed to determine a solution.
Thanks in advance.
I have a custom theme with a bunch of custom post types. I want to keep my "news" post type to display as a paginated list (that links directly to the external news article - already figured this part out), but I want to get rid of all single pages that get created when I add a news news post. Basically the opposite of this: How do I create new content pages for my Custom Post Type?. I apologize if this as too vague, not a developer. Just let me know what additional info is needed to determine a solution.
Thanks in advance.
Share Improve this question asked Sep 3, 2019 at 18:59 Mike KCMike KC 131 silver badge4 bronze badges1 Answer
Reset to default 1Because you are linking to external news articles you would like to prevent users from getting to the /news-article/article-title/
url that is created for a new post and would have no real content?
You could do something like this: How to disable the single view for a custom post type?
The code below is from that answer. Add it to your functions.php, making sure to change news_article
to the actual post type slug. This should 301 redirect a user trying to hit that single post. You may want to test this first using 302 as many browsers will cache a 301 and can be difficult to clear out.
It defaults to 302, so remove the , 301
to set it to default if you'd like to try with a 302.
add_action( 'template_redirect', 'news_article_redirect' );
function news_article_redirect() {
$queried_post_type = get_query_var('news_article');
if ( is_single() && 'news_article' == $queried_post_type ) {
wp_redirect( home_url(), 301 );
exit;
}
}