disable /feed/ for custom post type

Our custom post type posts have an extra feed in their source code:

www.example/post-type/post/feed/

I want to remove the extra /feed/ from our CPTs, as they generate 404s.

From the register_post_type function reference, I've tried adding 'rewrite' = array('feeds' = false), to:

register_post_type('templates',
    array(
    'labels' = array(
            'name' = __( 'Templates' ),
            'singular_name' = __( 'Template' ),
            'add_new' = __( 'Add New' ),
            'add_new_item' = __( 'Add New Template' ),
            'edit' = __( 'Edit' ),
            'edit_item' = __( 'Edit Template' ),
            'new_item' = __( 'New Template' ),
            'view' = __( 'View Template' ),
            'view_item' = __( 'View Template' ),
            'search_items' = __( 'Search Templates' ),
            'not_found' = __( 'No Templates found' ),
            'not_found_in_trash' = __( 'No Templates found in Trash' ),
            'parent' = __( 'Parent Templates' ),
        ),
      'public' = true,
      'show_ui' = true,
      'exclude_from_search' = true,
      'hierarchical' = true,
      'supports' = array( 'title', 'editor', 'thumbnail','page-attributes' ),
      'rewrite' = array('feeds' = false),
      'query_var' = true
    )
  );
}

but this has not resolved the issue.

Help appreciated.

Topic feed custom-post-types Wordpress

Category Web


From this post, I added

remove_action( 'wp_head', 'feed_links_extra', 3 );

to our child theme's functions.php.

Our SEO manager was okay with the fact this would remove feeds for categories as well.


If you dive into how this works, you end up at set_props. At the bottom of that function you see that setting rewrite' => array('feeds' => false) will be interpreted as 'no rewriting', meaning the default will be used. Also, if you have not set the has_archive argument WP will not know what to fill the feed with. This is what leads to your 404's.

That said, what you need is a function that intercepts queries for \feed\ before they lead to a 404. After all, you can't prevent people typing that url in their browser window, even if there would be a method to stop WP from generating it. So I'd do a check like this:

add_action ('template_redirect','wpse402292_redirect_feed');
function wpse402292_redirect_feed() {
  if (is_feed (array ('post','your-custom-post-type-name')))
     wp_redirect( home_url( '' ) ); // or somewhere else
  }

Paste this in your functions.php and replace your-cpt slug.

function wpse_191804_pre_get_posts( $query ) 
{
  // only for feeds
  if( !$query->is_feed || !$query->is_main_query() )
    return query;

  $exclude = 'your-cpt';
  $post_types = $query->get('post_type');

  if (($key = array_search($exclude, $post_types)) !== false)
    unset($post_types[$key]);

    $query->set( 'post_type', $post_types );

    return $query;
}

add_action( 'pre_get_posts', 'wpse_191804_pre_get_posts' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.