• Ok, so I have written my own custom post type plugin that also includes its own custom taxonomy (pretty much just categories that are specific to the CPT. Anyway, I cant for the life of me figure out how to get a permalink of domain.com/cpt-name/custom-taxonomy/postname/. Ive found some old solutions, but they all seem like really dirty hacks and are probably outdated. What is the proper and offical way of doing this? I of course already have the basic rewrite enabled for the cpt and taxonomy.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The basic rewrites when you declare post types or taxonomies only applies to URLs where the item is the only query term. It does not address special permalinks where the two are combined into one.

    In truth, if the permalink includes a post slug, the remaining terms are superfluous. The slug is all you need to get the correct post. Anything else could create query conflicts resulting in nothing being found.

    The easiest way to combine terms is to start with a basic permalink resulting in a single query term, then add the other terms as URL parameters, for example /cust-tax/my-tax-term/?post_type=my-cpt . URL parameters are never ambiguous. Combined terms in the permalink can be ambiguous unless key names are always present.

    Ambiguous: /cpt-name/tax-term/ or /tax-term/cpt-name/ ?
    Clear: /custom-tax/tax-term/cpt/post-slug/ where the term always follows a fixed keyname, shown in italics here.

    With fixed key names, you can use add_rewite_rule() to cause WP to properly parse multi-term permalinks. You can still use this function without fixed key names, the key name based on position alone, like the taxonomy term is always the third permalink term in all permalinks, but then you need to ensure all permalinks adhere to this format.

    add_rewite_rule() is the proper and accepted way to handle complex permalinks. I would not say it’s the ‘official’ way because there is no document saying you must use this for this situation.

Viewing 1 replies (of 1 total)
  • The topic ‘CPT's and proper permalinks’ is closed to new replies.