Rewrite URL with category and tag combined using WP_Rewrite

These should work for you:

function myplugin_rewrite_tag_rule() {
    add_rewrite_rule(
        'category/(.+?)/tag/([^/]+)/?$',
        'index.php?category_name=$matches[1]&tag=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        'category/(.+?)/tag/([^/]+)/page/?([0-9]{1,})/?$',
        'index.php?category_name=$matches[1]&tag=$matches[2]&paged=$matches[3]',
        'top'
    );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);

The second rule is to support pagination so you can have:

https://example.com/category/healthy-living/relationships/tag/flock

as well as:

https://example.com/category/healthy-living/relationships/tag/flock/page/2/

Note that you don’t need to add a rewrite tag, as it’s already added by core.

Leave a Comment