• allm

    (@realblueorange)


    I’ve been spending a lot of time on getting hierarchical customs posts to work. It looks like the rewrite rules do not work for that.

    The custom post used to be non-hierarchical and that worked just fine. After that I changed it to hierarchical and flushed the rewrite rules. I can see that the rewrite rules have changed.

    But… the permalinks do not work anymore (result: 404).

    Setting the custom post back to non-hierarchical (and flushing) makes everything work again.

    I have set the permalink settings to postname. If I change it to plain all is well for the hierarchical custom post, but plain is not what I want.

    I’ve searched the forum but have not found a solution to this.

    Is this a known bug? Am I doing something wrong? Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You haven’t told us what sort of permalink you’re after. I’m guessing it’s like this:
    example.com/parent-post/child-post/grand-child-post/

    Such a permalink going 404 without special handling would be expected. The variable length nature of this permastruct makes rewrite rules difficult.

    OTOH, if what you’re after is simply
    example.com/grand-child-post/

    and it’s going 404, then that is something else entirely. There is a glitch with your installation. This should work by default with any “pretty” permastruct.

    I’m not very good with rewrite rules, but I think something like this might work (untested):

    function custom_rewrite_rule() {
       add_rewrite_rule('/([^/]*)/?$','index.php?name=$matches[1]','top');
    }
    add_action('init', 'custom_rewrite_rule');

    Don’t forget to flush ??

    Thread Starter allm

    (@realblueorange)

    @bcworkz

    Thanks for looking into this!

    While registering the custom post I use this as one of the arguments:

    'rewrite' => array(
            'slug' => '/abc',
            'with_front' => false,
            'feeds' => false,
            'pages'=> false
    ),

    This will result in permalinks like:

    https://www.example.com/abc/postslug
    https://www.example.com/abc/postslug/childslug

    and so on.

    The default rewrite rules that are being generated for handling this do not match the permalinks that WordPress itself generates. That sounds strange to me.

    I’ll have a look at writing my own rewrite rules (new territory), but one would expect that WordPress generates the rewrite rules that match the permalinks that it generates.

    Thanks for the piece of code you provided. I’ll have a go at that. I’ll also have a look at “ep_mask” that is one of the arguments when registering the custom post…

    Any more ideas are welcome. If I get this fixed I’ll report it here. And if not I will go with a flat custom post structure and fix it another way…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘rewrite rules for hierarchical custom posts’ is closed to new replies.