Make WordPress Core

Opened 7 years ago

Last modified 6 years ago

#43746 reviewing defect (bug)

Custom post type single post feed returns a 404 if has_archive is set to false when calling register_post_type()

Reported by: henrywright's profile henry.wright Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: Future Release Priority: normal
Severity: normal Version: 4.9.5
Component: Rewrite Rules Keywords: 2nd-opinion has-patch needs-testing needs-unit-tests
Focuses: Cc:

Description

When using register_post_type(), the single post feed returns a 404 if has_archive is set to false. This seems to happen regardless of the value of feeds in the rewrite array. For example:

register_post_type(
    'example',
    array(
        'rewrite'     => array( 'slug' => 'my-custom-post-type', 'feeds' => true ),
        'has_archive' => false
    )
);
$ curl -IL https://example.com/my-custom-post-type/some-text/feed/
HTTP/1.1 404 Not Found

I'd expect a feed for the post to be built if feeds is set to true.

Note I have flushed permalinks.

Attachments (2)

43746.diff (4.4 KB) - added by soulseekah 7 years ago.
43746.2.diff (1.5 KB) - added by mattkeys 6 years ago.

Download all attachments as: .zip

Change History (12)

#1 @soulseekah
7 years ago

  • Component changed from Feeds to Rewrite Rules

Hey, thanks for your report.

This appears to be by design. Types that have no archive will not create feed endpoints as you would expect.

Singe post and page feeds are comment feeds and are created separately inside WP_Rewrite::rewrite_rules, specifically in WP_Rewrite::generate_rewrite_rules.

By extension, you are able to create your own custom feed rules on single types by using WP_Rewrite::generate_rewrite_rules and hooking into the rewrite_rules_array filter.

A quick fix would be to do:

add_action( 'rewrite_rules_array', function( $rules ) {
	global $wp_rewrite;
	$wp_rewrite->add_rewrite_tag( '%my-custom-post-type%', 'my-custom-post-type/([^/]+)', 'example=' );
	$custom_rules = $wp_rewrite->generate_rewrite_rules( '%my-custom-post-type%', EP_NONE, true, true, false, false );
	return $custom_rules + $rules;
} );

This will generate the needed feed. I am working on a leaner patch.

P.S.: The Rewrite API is insane to work with.

@soulseekah
7 years ago

#2 @soulseekah
7 years ago

  • Keywords has-patch 2nd-opinion added

43746 is an attempt at providing this feature in register_post_type by introducing a rewrite key called single_feed which doesn't care about the has_archive property.

#3 in reply to: ↑ description @SergeyBiryukov
7 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Review to 5.0

Replying to henry.wright:

I'd expect a feed for the post to be built if feeds is set to true.

I agree, I don't think we need a new argument here.

The existing feeds argument could be updated to support single post (comments) feed. If has_archive is disabled, it would be the only feed generated.

@soulseekah Thanks for the patch! Could you update it to decouple the feeds argument from has_archive?

Last edited 6 years ago by SergeyBiryukov (previous) (diff)

#4 @mattkeys
6 years ago

Just to add to this discussion, I found this ticket when searching for solutions to why a 404'ing feed is being added to the head on a site I manage.

The comments feed link tag seems to be added to the head so long as comments are open on the post type, which they are. However because I have has_archive set to false, the feed 404's, creating SEO/Crawl issues. After reading the docs on 'register_post_type' I found the 'feeds' param under rewrites and set it to true, expecting it to fix the problem, but it did not, and I eventually found my way here.

@mattkeys
6 years ago

#5 @mattkeys
6 years ago

I just added a new patch that would allow for the expected behavior of feeds => true, decoupling it from has_archive.

A new ticket will probably need to be opened to prevent the comment feed link tag from being added to the head on sites when comment feed permalinks are not being generated because has_archive = false and feeds = false.

Last edited 6 years ago by mattkeys (previous) (diff)

#6 @mattkeys
6 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

This ticket was mentioned in Slack in #core by mattkeys. View the logs.


6 years ago

#8 @SergeyBiryukov
6 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#9 @peterwilsoncc
6 years ago

  • Milestone changed from 5.0 to 5.1

Moving to the 5.1 milestone due to the WordPress 5.0 focus on the new
editor (Gutenberg).

#10 @pento
6 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from 5.1 to Future Release

Patch needs testing and review.

Note: See TracTickets for help on using tickets.