My question comes from this one. I have never really touched on rewrite rules as frankly, they still do not make sense to me

What I have

I have successfully build the following link


which should, by default, lead to a 404 page, as custom-archive does not exist. It is the same as visiting


That part works fine.

Now, what happens, this link comes and only shows on a single post when the single post has categories and tag attached to it. If the post has categories and/or tags, I add the relevant category ID's and tag ID's to the link through custom query_vars. The link then looks like this



  • I can, using template_include that whenever either one of the two query_vars exist, use a custom template. This works

  • By having a custom query on that specific template, display the correct posts


Although I get posts with a custom query ( which I also want to eliminate ), the main query 404's. This can be seen in my breadcrumbs and in the browser tab which displays "Page not found"


I have used pre_get_posts to get the values from my custom query_vars to alter the main query, but this does not work as Wordpress tries to read the page as post or single page

I have also tried rewrite rules, which also does not help my cause. The problem here is, I really can't get the correct combination. I know I have to somehow tell Wordpress that this page is a custom archive and not a post or a page and then use the values from my custom query_vars in a tax_query to get posts that belongs to the categories AND tags given in the custom query_vars

Any suggestions to the correct rewrite rules to achieve this?


I know I have to somehow rewrite


to something where Wordpress can understand that this an custom archive page (or vitual page) for the post type post, and the posts should that should be displayed on the page should match posts that are in the categories given by cq AND has tags which is given by tq.

This is a basic idea of what works in my custom template with a custom query

$args = [
    'post_type'    => 'post',
    'tax_query'    => [
        'relation' => 'AND',
            'taxonomy' => 'category',
            'terms'    => get_query_var( 'cq' ),
            'taxonomy' => 'post_tag',
            'terms'    => get_query_var( 'tq' ),
$q = new WP_Query( $args );

I need to translate this to work for the main query. Simply adding the above to pre_get_posts does not work, the page simply 404's, so somehow the URL needs to be rewritten for this to work

1 Answer 1


You can create a posts archive by just setting post_type=post in your rewrite rule:

function custom_archive_rule() {
add_action( 'init', 'custom_archive_rule' );

WordPress will identify this as is_home, so you'll have to target it in pre_get_posts by the existence of your extra query variables.

function custom_arcive_tax( $query ) {
    if ( $query->is_home()
        && $query->is_main_query()
        && isset( $_GET['cq'] ) ) {
            $tax_query = array(
                    'taxonomy' => 'category',
                    'terms' => $_GET['cq'],
            $query->set( 'tax_query', $tax_query );
add_action( 'pre_get_posts', 'custom_arcive_tax' );

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.