1

I'm trying to link to custom post types via url. So I have CPT 'forums' (example.com/forums) and CPT 'topics' (example.com/topics)

I am trying to achieve a url of the two combined (for CPT 'topics') plus adding meta data in between (example.com/forums/forumname/topic/topicname) is this possible?

    //register post type
    register_post_type( 'forums', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail','page-attributes' ),
    'taxonomies' => array(  ),  
    'exclude_from_search' => false,
    'capability_type' => 'page',
    'rewrite' => array( 'slug' => 'forums' ),
    )
);

which works with the correct url (example.com/forums/).

//register post type
register_post_type( 'topics', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail','page-attributes' ),
    'taxonomies' => array( 'post_tag'), 
    'exclude_from_search' => false,
    'capability_type' => 'post',
    'rewrite' => array( 
        'slug' => 'forums/topic',
        'with_front' => true
         ),
    )
);

which breaks the permalinks for CPT 'topics'. I don't understand URL rewriting very well and need someone to walk me through it a little, or at least point me in the right direction. I have been reading guides but I just get more confused.

2

They key thing to understand about rewrite rules is that they only work as long as they are perfectly unambiguous.

You have an idea in your head what example.com/forums/forumname/topic/topicname means so it makes sense to you. When WP gets this URL it has no remote clue what it means, it's just a bunch of words. Then it goes to rewrite rules and seeks the rule that will "translate" it to the context it does understand (query variables).

So as soon as URL can be explained by more than one rule it all breaks down. For native post types and taxonomies it is all carefully designed so that rules don't overlap. Such as categories have to have /category/ and so on.

With custom stuff you have more freedom to define things and so more freedom to break things by creating overlapping rules.

So in general you should just avoid any overlap in slugs and such, as much as possible.

If you do need overlap your life gets harder. It is still doable, but you will have to dive quite deeply to debug rules being created. Likely you will also need to construct more tight rewrite rule for your situation from scratch, since "easy" way with registration functions probably won't cut it.

  • Thanks for the reply Rarst. Can you point me to any good guides for noobies? – lukgoh Sep 26 '17 at 15:03
  • 2
    Not really, expert people tend to run away from WP rewrite implementation screaming. :) Monkeyman Rewrite Analyzer plugin is my staple to see what is going on with the rules wordpress.org/plugins/monkeyman-rewrite-analyzer – Rarst Sep 26 '17 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.