1

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.

8
  • 1
    Set it to false instead of null.
    – rudtek
    Commented Feb 7, 2022 at 5:53
  • @rudtek, I made that change but the extra feed remained. There is no WP server cache installed, and I cleared my browser cache and did a hard refresh.
    – Steve
    Commented Feb 8, 2022 at 1:30
  • You may need to visit your permalinks page (settings/permalinks) to refresh rewrites.
    – rudtek
    Commented Feb 8, 2022 at 5:07
  • Thanks, @rudtek, I resaved my permalinks, but the issue remains.
    – Steve
    Commented Feb 8, 2022 at 5:32
  • Ticket is already being generated related to this topic
    – KNT111_WP
    Commented Feb 8, 2022 at 5:36

3 Answers 3

1

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
  }
1
  • I would prefer the extra feed not to be present in the page. If it isn't present, I am happy with it generating a 404. Cheers.
    – Steve
    Commented Feb 10, 2022 at 4:42
0

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.

-1

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' );
3
  • Where did you find that code from?
    – Steve
    Commented Feb 8, 2022 at 6:10
  • wordpress.stackexchange.com/questions/191804/…
    – KNT111_WP
    Commented Feb 8, 2022 at 6:10
  • The problem in that post (I would like to remove all posts with a post type of nf_sub from showing up in any RSS feeds) is different to my problem (remove a custom post type's feed).
    – Steve
    Commented Feb 8, 2022 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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