0
votes

I can't seem to figure this out from the documentation.

I have a wordpress site with a custom post type called "News"

There are 21 "posts" with data that we are just showing on the root level permalink /news/

However each of those 21 posts have their own page/slug that you can click "view"

This brings you to a blank page because we never built a custom page to show that data... again we are just showing the data on the /news/ and want to keep that.

But we want to disable the actual creating of those pages with each of their own permalinks for SEO purposes..

So, basically I want "/news/the-custom-news-post-that-i-made-example" to redirect to the 404 page.. or not redirect at all.. it just shouldn't exist.

Any help with be much appreciated in advance!

When I set the register_post_type - public to false, or publicly_queryable to false this is not giving me the results I want.. bc then I can't see /news/ anymore either.

Thanks -O

Edit..

Do you mean editing this code... not sure what you mean by your wp-admin..

  <?php
add_action('init', 'theyard_news_init');
add_action('init', 'create_taxonomy');

function theyard_news_init() {
    $labels = array(
        'name'               => __('News', 'theyard'),
        'singular_name'      => __('News', 'theyard'),
        'add_new'            => __('Add New', 'theyard'),
        'add_new_item'       => __('Add New news', 'theyard'),
        'edit_item'          => __('Edit News', 'theyard'),
        'new_item'           => __('New News', 'theyard'),
        'all_items'          => __('All News', 'theyard'),
        'view_item'          => __('View News', 'theyard'),
        'search_items'       => __('Search News', 'theyard'),
        'not_found'          => __('No News found', 'theyard'),
        'not_found_in_trash' => __('No News found in Trash', 'theyard'),
        'parent_item_colon'  => '',
        'menu_name'          => __('News', 'theyard')
    );
    $args   = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => false,
        'capability_type'    => 'page',
        'rewrite'            => true,
        'has_archive'        => 'news',
        'hierarchical'       => false,
        'menu_position'      => 21,
        'menu_icon'          => 'dashicons-media-text',
        'supports'           => array('title', 'thumbnail', 'editor'),
        'show_in_nav_menus'  => true,
        'show_in_admin_bar'  => true,
        'taxonomies'         => array()
    );
    register_post_type('news', $args);
}
2
you just need to check in archive page if post type is news than load the 404 page template. - Akhilesh
But I want to keep the archive page.. I want to 404 the single view pages - MuhuPower
yes you only need to add if condition and load the templates based on that. - Akhilesh
Can you show me an edited version of the code above, with the If statement.. not sure how to do it.. - MuhuPower

2 Answers

0
votes

https://wordpress.stackexchange.com/questions/128636/how-to-disable-the-single-view-for-a-custom-post-type

<?php
add_action( 'template_redirect', 'wpse_128636_redirect_post' );

function wpse_128636_redirect_post() {
  $queried_post_type = get_query_var('post_type');
  if ( is_single() && 'sample_post_type' ==  $queried_post_type ) {
    wp_redirect( home_url(), 301 );
    exit;
  }
}
?>

You obviously need to create and archive-news.php as per the Template Hierarchy for CPTs

0
votes

just add this code to check if post type is news then render 404 template else render content template.

if ( is_single() && get_post_type() == 'news' ) {
    get_404_template();
} else {
    get_template_part( 'content', get_post_format() );
}

you can get more info about how pages works in wordpress from this link - https://developer.wordpress.org/themes/template-files-section/post-template-files/

or you can create a template 404 and then assign it in your custom post type and now it will always render 404 page and this is how you can create templates in wordpress https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/

also you can create single-news.php page for your news post type which simply loads the 404 page ref - https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/