I have an archive for each category/tag. Now I want to create a custom archive using the following URL structure:


This should show all posts that have the tag tag1 and belong to cat1 and subcat1

How can I create this archive?

1 Answer 1


What you are looking for is a custom search that can be created via rewrite rules. What I'm gonna do is to create a rule that adds some queries to the URL.

First, let's define a rewrite tag so that WordPress recognizes our custom URL.

function my_rewrite_tag() {
  add_rewrite_tag('%custom-categories%', '([^&]+)');
add_action('init', 'my_rewrite_tag', 10, 0);

Now, we create a rule to redirect our data to another path.

function my_rewrite_rule() {
add_action('init', 'my_rewrite_rule', 10, 0);

So if we try and access:


we will actually be redirected to:


Which is the archive for normal post types. Now is the trick. We will alter the query if these data are set:

// We only want to do this if every data is set, so let's check for them all
if(isset($_['customCat']) && isset($_['customSubCat']) && isset($_['customTag1']) && isset($_['customTag2'])) {
    // Now alter the main query
    function photogram_include_blog_posts($query) {
        if ( !is_admin() && $query->is_main_query() && $query->is_archive() ) {
            // Set the tags
            $query->set('tag__in', array($_['customTag1'],$_['customTag2']));
            // Set the categories
            $query->set('category__in', array($_['customSubCat'],$_['customCat']));
    add_action( 'pre_get_posts', 'photogram_include_blog_posts' );

So we are redirected to an archive page that runs our custom query. Done.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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