6

I try to add tax query on pre_get_posts hook using following code :

    // Add custom posts to main query (archive and home)
add_action( 'pre_get_posts', 'add_post_types_to_query' );

function add_post_types_to_query( $query ) {
    if ( (is_home() || is_category() || is_search()) && $query->is_main_query() && !is_admin() )
    {
        // Query all posts on index and category pages
        $query->set( 'post_type', array( 'post', 'page', 'definition', 'video', 'data_vizualization', 'quizz', 'talk', 'question', 'study', 'bibliography' ) );

        $workspace_taxonomy_query =
                    array(array(
                        'taxonomy ' =>  'workspace',
                        'field'     =>  'slug',
                        'terms'     =>  array('particulier'),
                        'operator'  =>  'IN'
                    ));
        $query->tax_query->queries[] = $workspace_taxonomy_query;
        $query->set( 'tax_query', $workspace_taxonomy_query);
    }
    return $query;
}

This code give the following where clause : "0 = 1". What is wrong? Is this the good hook? How can I properly add taxonomy filter BEFORE main query is done?

Thanks in advance for answers !

5
  • 3
    +1 for using pre_get_posts!
    – Tom J Nowell
    Apr 20, 2015 at 17:29
  • There's nothing in your code that should cause 0 = 1 - I think something else is at play here. Apr 20, 2015 at 17:46
  • 1
    0 = 1 happen when the taxonomy does not exist. Check the spelling, maybe
    – gmazzap
    Apr 20, 2015 at 18:15
  • is the taxonomy term 'particulier' right? or should it be 'particular'? Not sure if that could cause 0 = 1 but just checking.
    – Jen
    Apr 21, 2015 at 20:40
  • Thx for pointing it, but I tried with many terms and it didn't work either. Furthermore, even if term not exists, it shouldn't be a problem shouldn't it? Apr 22, 2015 at 8:08

1 Answer 1

3

You have a syntax-ish error, or a spelling one. There is a space after "taxonomy" in your tax_query array. If that were the sum of it I'd have just posted a comment, but additionally that $query->tax_query->queries[] = $workspace_taxonomy_query; line is pointless. Nothing changes with or without it. Don't try to hack the array like that. Just use $query->set( 'tax_query', $workspace_taxonomy_query); as below (using a taxonomy and term that exists in my test environment):

// Add custom posts to main query (archive and home)
add_action( 'pre_get_posts', 'add_post_types_to_query' );

function add_post_types_to_query( $query ) {
    if ( (is_home() || is_category() || is_search()) && $query->is_main_query() && !is_admin() )
    {
        // Query all posts on index and category pages
        $query->set( 'post_type', array( 'post', 'page', 'definition', 'video', 'data_vizualization', 'quizz', 'talk', 'question', 'study', 'bibliography' ) );

        $workspace_taxonomy_query =
                    array(
                      array(
                        'taxonomy' =>  'post_tag',
                        'field'     =>  'slug',
                        'terms'     =>  array('test-4'),
                        'operator'  =>  'IN'
                      )
                    );
        $query->tax_query->queries[] = $workspace_taxonomy_query;

    }
    return $query;
}
1
  • You were just right, it was this space that messed up everything... Many thanks for your contribution May 13, 2015 at 14:00

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.