0

I have created a custom taxonomy, and that works.

But here is the problem; I have created an archive page, which in the url gets e.g. this: ?category=23. Then, in functions.php, I do this:

function add_inspiration_category( $q ) {
    if (is_post_type_archive('inspiration') && !is_admin()) {

        $category_id = $_GET['category'];
        if (!empty($category_id)) {
            $q->set('tax_query', array(
                array(
                    'taxonomy' => 'inspiration-taxonomy',
                    'field' => 'id',
                    'terms' => $category_id
                )
            ));
        }
    }

    return $q;
}
add_filter('parse_query', 'add_inspiration_category');

So, basically I'm filtering the archive pages result based on the id in the url. This works, except when I'm filtering on a category, the main menu dissapears from the header. If I remove the ?category=23 from the url or comment out the section above where I set the tax_query, it works as expected again. Anyone know what this is?

2

You are applying that filter to every query on the pages matching the conditions or that if. You need to restrict this to only the main query and make sure that the conditions are only operating on the $q query passed into the function.

if ($q->is_main_query() 
   && $q->is_post_type_archive('inspiration') 
   && !is_admin()) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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