Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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()) {
share|improve this answer
    
Thank you, that worked! –  kjelelokk Feb 8 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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