Custom permalink structure for custom post types including multiple taxonomy

This code produces the right structure but the post can’t be found
(404)

That is because the custom rewrite tags (%filter_1% and %filter_2%) appeared as-is in the generated rewrite rules (in the database). So you need to register the tags so that they’re replaced with the proper RegEx (regular expression) pattern when WordPress (re-)generates the rules.

And you can register them using add_rewrite_tag() like so:

function register_work() {
    // Register the custom rewrite tags.
    add_rewrite_tag( '%filter_1%', '([^/]+)' );
    add_rewrite_tag( '%filter_2%', '([^/]+)' );

    // ... then register the post type.
}

And that should fix the error, but you should know that your post type’s rewrite rules will result in issues like Pages (posts of the page type) showing the homepage instead of the correct Page.

So you should just use a unique rewrite base, e.g. work as in 'slug' => 'work/%filter_1%/%filter_2%' to avoid clashes with other rewrite rules, particularly the default ones in WordPress.

However, if you really must use no rewrite base there, then there is a way.

  • But each time after you created or deleted a filter term, or maybe changed its slug, you need to manually flush the rewrite rules by simply visiting the permalink settings page.

    Note: Yes there is a programmatic way to automatically flush the rewrite rules, but doing it manually is really easy, so just create/edit/delete your filter terms in bulk and then manually flush the rewrite rules. 🙂

  • And there should be no posts (Pages, Posts, CPTs) or terms having the same slug as used by your filter terms. I.e. If you have a filter term with the slug foo, then you should have no posts or terms (in other taxonomies) having the slug foo.

    But what I really mean is, you can use the same slug, but not in all cases and be wary. For example, if a work CPT post has foo/bar/baz as the permalink, then there should be no Page with that path, i.e. a Page with the slug baz that’s a child of the bar and foo Pages.

So if those are good to you, then here’s the code you can try:

add_filter( 'rewrite_rules_array', function ( $rules ) {
    $terms = get_terms( [
        'taxonomy'   => 'filter',
        'hide_empty' => false,
        'fields'     => 'slugs',
    ] );

    if ( ! is_wp_error( $terms ) ) {
        foreach ( $terms as $slug ) {
            $match = "^{$slug}/(.*?)/([^/]+)(?:/(\d+)|)/?$";
            $query = 'index.php?post_type=work&work=$matches[2]&name=$matches[2]&page=$matches[3]';
            $rules[ $match ] = $query;
        }
    }

    return $rules;
} );

And in addition to that code:

  • In register_work(), use work as the base: 'slug' => 'work/%filter_1%/%filter_2%',

  • Then in the custom_permalinks() function:

    // You need to replace this:
    $find = array('%filter_1%','%filter_2%');
    
    // with this:
    $find = array('work/%filter_1%','%filter_2%');