Wordpress rewrite rule for archive

So I have a problem with the rewrite rules in WordPress.

When I enter a custom request that is not there, it goes back to index.php, but I don't want it because I am handling the request variables in the archive- (page) .php file.

<?php
    add_filter('query_vars', 'cat_query_vars');
    function cat_query_vars($qvars) {
        $qvars[] = 'a_type';
        return $qvars;
    }

    add_action('init', 'create_type_rewrite');
    function create_type_rewrite() {
        add_rewrite_rule('^atype/([^/]+)/?', 'index.php?post_type=cars&a_type=$matches[1]', 'top');
    }
?>

      

If I put "Mercedes" and it is in the system, it will go to archive-cars.php and use get_posts ($ args) where I have "a_type" => $ wp_query-> query_vars ['a_type "] ;. There is global $ wp_query.

I still have a problem, say if I have a normal Mercedes taxonomy in there, it will bring up the results for everyone related to Mercedes, but if I put sadfasdf it will be indexed by default. php for some reason.

+3


source to share


1 answer


You add queries like this ...

function add_my_query_var($vars) {
    $vars[] = 'my_var';
    return $vars;
}

add_filter('query_vars', 'add_my_query_var');

      



the filter function is passed in an array of existing requests, you need to add this array and then return it.

0


source







All Articles