最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

disable feed for custom post type

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Feb 8, 2022 at 3:21 Steve asked Feb 7, 2022 at 5:34 SteveSteve 1,75719 gold badges66 silver badges115 bronze badges 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
 |  Show 3 more comments

3 Answers 3

Reset to default 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
  }

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.

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' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论