WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#43746 new 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: henry.wright Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.9.5
Component: Rewrite Rules Keywords: 2nd-opinion needs-patch
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 (1)

43746.diff (4.4 KB) - added by soulseekah 2 months ago.

Download all attachments as: .zip

Change History (4)

#1 @soulseekah
2 months 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
2 months ago

#2 @soulseekah
2 months 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
2 months 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 updated it to decouple the feeds argument from has_archive?

Note: See TracTickets for help on using tickets.