2

What happens if in

register_taxonomy( $taxonomy, $object_type, $args )

I set query_var arguement to true?

I read here this

query_var

(boolean or string) (optional) False to disable the query_var, set as string to use custom query_var instead of default which is $taxonomy, the taxonomy's "name".

Default: $taxonomy

Note: The query_var is used for direct queries through WP_Query like new WP_Query(array('people'=>$person_name)) and URL queries like /?people=$person_name. Setting query_var to false will disable these methods, but you can still fetch posts with an explicit WP_Query taxonomy query like WP_Query(array('taxonomy'=>'people', 'term'=>$person_name)).

Could someone please explain?

  • 1
    What don't you understand exactly about the above. I think it is quite explicit in explaining what query_var does – Pieter Goosen May 19 '15 at 11:22
  • 1
    I mean what happens if you do this 'query_var' => true,? – kanenas May 19 '15 at 11:32
5

I'm not sure how to explain this in more detail, or better than it is already explained in the codex, but here is a few notes

  • IMHO, 'query_var' => true adds no proper value to your taxonomy or any other functionality relating to your taxonomy

  • As I read the docs, setting query_var to true, it makes it possible to query posts from a given term as follow

    $args = [
        '{tax}' => '{term-slug}'
    ];
    $q = new WP_Query( $args );
    

    First of all, this syntax is depreciated as of version 3.1 and is replaced with a proper tax_query which has much more advantages over the above syntax like nesting, querying posts with different relationships and including and/or excluding certain terms etc etc. The proper syntax now is

    $args = [
        'tax_query' = [
            'relation' => 'AND' // or 'OR' . RELATIONSHIP BETWEEN TWO OR MORE SETS OF TAX ARRAYS
            [
                'taxonomy' => '{tax}',
                'field' => 'CAN BE term_id TO PASS TERM IDS, slug FOR TERM SLUGS OR name FOR TERM NAMES',
                'terms' => 'TERM VALUES ACCORDING TO field SET',
                'include_children' => true OR false, // true INCLUDES ALL CHILD TERMS OF GIVEN TERM, false EXCLUDES CHILD TERMS
                'operator' => 'IN' // or 'NOT IN'. SHOULD POSTS BE IN THE GIVEN TERM OR NOT
            ],
        ],
    ];
    $q = new WP_Query( $args );
    

    Secondly are all the advantages of the tax_query syntax as you can see from the above. With version 4.1 came a complex nesting feature which let you build even more complex tax queries

  • Lastly, if setting query_var to true makes the relevant query variables available for use on taxonomy pages (I'm not even sure if it does), it is in fact still useless as the current queried object can be accessed with get_queried_object()

So to conclude, there is really no advantage setting query_var to true. It is purely personal choice if you want to waste time and space writing it out

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.