0

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.

0

Because 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;
  }
}
  • Exactly. I saw that solution as I was searching for an answer. So, the page still gets created, I am just redirecting any link to it, correct? – Mike KC Sep 4 at 20:38
  • Yep, the page will still get "created". WordPress will generate a post object that contains all the standard data for a post (id, slug, title, etc) when you add a news article. With the way template hierarchy works, no matter how many template files you try to remove to sort of cheat your way into not generating a page with markup, you'll always end up back at index.php being used to spit out something. So the best way to avoid a user hitting content that technically exists, is to redirect it somewhere. Your users won't accidentally hit those posts if you never link to them form anywhere. – zfors Sep 5 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.