Permalinks: custom structure for taxonomy – tags?

In my WordPress permalink settings I have set the Tag-Base to “”. So /tag/whatever is the current-url structure for my tag-page.

However I do have a custom-post-type with a custom-taxonomy that is also using this as rewrite_slug.

Read More
register_taxonomy(
        'event_tags',
        'wr_event',
        array(
            'label' => 'Tags',
            'singular_label' => 'Tag',
            'hierarchical' => false,
            'query_var' => true,
            'rewrite' => array('slug' => 'tag'),
        )
    );

This means both (tags for normal posts and tags for my custom-post-type) should have the same permalink structure for their tags.

However this does not work. When I do set the rewrite-slug for my custom-taxonomy to “tag” as well, the normal tag-archive for blogposts throws a 404.

I know I could easily fix this by just using two different rewrite-bases for both post-types. However I wonder if it is possible to make this work as well. So that normal tags of blogposts and tags of my custom-post-type do have the same rewrite structure.

Any ideas on that?

Related posts

Leave a Reply

1 comment

  1. If you hook into the ‘rewrite_rules_array’ filter, do a print_r on $rules, and you’ll see the huge preg-match key-value pairs.

    add_filter( 'rewrite_rules_array','mess_with_rewrite_rules' );
    function mess_with_rewrite_rules( $rules ) {
            print_pre($rules);
        return $rules;
    }
    

    See where your rule is in the stack. WordPress loops through this list looking for anything that matches the keys, which are regex patterns, and applies the captured values to the associated value via a preg_match(). I don’t know for sure, but I suspect that on the first match, it stops looking. So if tags in the post_tag sense, it stops looking. Thats probably why its not reaching yours.

    If im wrong, then it means WordPress doesnt stop looking, but I doubt thats the case. Since the tag doesnt exist in your post_tag taxonomy, it returns a 404. Try setting up a term in post_tags with the same slug and see if you end up landing on that term.

    I dont think theres a way to do what your asking. The only way WordPress can know what your looking for via the URI is the pattern of the permalink. If you’ve got two things with the same pattern, then there is no information to differentiate the two.

    Solution A:
    Use a hierarchical taxonomy instead.
    Turn tags into a hierarchical taxonomy which can house both sets of terms. The primary problem I suspect would then be the user interface. By making post_tags hierarchical, the metabox will switch to the category-style checkbox list. If you want to maintain the non-hierarchical tag structure, you’ll probably need create a custom metabox to only contain children of ‘event_tags’ and another for all the others.

    Solution B:
    Change something about your ‘event_tags’ pattern. For example you could for example, have /tag/events/. Then you would insert a special pattern to look for /tag/events/(.+?)$%i. By inserting this at the top of the rewrite rules array, it will match first and stop looking. You can then apply the matched term to index.php?taxonomy=event_tags&term_slug=$matches[1] This would work, however it would break any post_tag whose slug happened to be ‘events’.

    Note: I don’t know if term_slug is the correct query variable, I’m sort of guessing. Look that up by looking at the rewrite rules array, and finding an example of other uses of custom taxonomies.