Make WordPress Core

Opened 2 years ago

Last modified 20 months ago

#35916 new defect (bug)

WP_Rewrite::generate_rewrite_rules() ignores boolean $endpoints / $feed parameters for CPT

Reported by: solo14000 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description (last modified by johnbillion)


I'm trying to declare custom rewrite rules for a custom post type without endpoints and feeds. As result, WP ignore the endpoint flag in the WP_Rewrite::generate_rewrite_rules().

What I did is as following.

I created a custom post type called 'event' with the following arguments for register_post_type() :

array( // Array of WP post type args
        'labels' => array(
                'name' => 'Events',
                'singular_name' => 'Event',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => false,
        'rewrite' => false,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-tickets-alt',
        'supports' => array(
        'taxonomies' => array(

Then I called

$args_post_type = array(
        'feed'          => false,
        'paged'         => false,
        'ep_mask'       => EP_NONE,
        'endpoints'     => false,
add_permastruct('events', 'events/%event%', $args_post_type);

The resultings rewrite rules are :

'events/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)
'events/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37)
'events/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50)
'events/[^/]+/attachment/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43)
'events/([^/]+)/embed/?$' => string 'index.php?event=$matches[1]&embed=true' (length=38)
'events/([^/]+)/trackback/?$' => string 'index.php?event=$matches[1]&tb=1' (length=32)
'events/([^/]+)(?:/([0-9]+))?/?$' => string 'index.php?event=$matches[1]&page=$matches[2]' (length=44)
'events/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)
'events/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37)
'events/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49)
'events/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50)
'events/[^/]+/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43)

Looking at class-wp-rewrite.php, I noticed in generate_rewrite_rules() that piece of code that forces the $post flag for CTP

if ( ! $post ) {
        // For custom post types, we need to add on endpoints as well.
        foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
                if ( strpos($struct, "%$ptype%") !== false ) {
                        $post = true;

                        // This is for page style attachment URLs.
                        $page = is_post_type_hierarchical( $ptype );

and that piece of code that unconditionnaly add extra endpoints / feeds... for post

// If we're matching a permalink, add those extras (attachments etc) on.
if ( $post ) {
        // Add trackback.
        $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);

Could confirm that problem?


Change History (3)

#1 @johnbillion
2 years ago

  • Component changed from Permalinks to Rewrite Rules
  • Description modified (diff)
  • Keywords needs-patch needs-unit-tests added
  • Version 4.4.2 deleted

Thanks for the report, @solo14000, and welcome to WordPress Trac.

I've confirmed this bug. Rewrite rules for endpoints and feeds still get generated for the permalink structure in this situation.

Last edited 2 years ago by johnbillion (previous) (diff)

#2 @thomascharbit
2 years ago


can you also confirm that the codex seems wrong about what argument to use to disable pagination?

On: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments

It says to set pages argument to false, while it should be paged according to what add_permastruct() does?

I don't know if the docs should be edited or if the pages argument should be handled, like it's done for feeds (which is mapped to feed in register_post_type())

#3 @solo14000
20 months ago

Registering custom post type with argument '_builtin' = true seems to be a workaround. Can anybody tell me more about side effects setting '_builtin' true?


Note: See TracTickets for help on using tickets.