I’m a little bit confused, why I can’t include all the terms in tax_query automatically?
My code:
'tax_query' => array(
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => array( nyc, boston, london ),
)
)
What If I’ve dozens of cities? Isn’t it uncool to add them all manually?
Unless you have posts that don’t have a city aren’t you essentially querying all your posts? Anyway, Eric Holmes is correct that you should be using
WP_Query
in lieu ofquery_posts()
.That said, you can get a list of all the terms in a taxonomy and then use those values in your tax query.
In addition to the marked ‘best answer’, there is another way which might be even faster because we don’t have to pull the terms and extract the names first.
This is achieved with a trick by using a non-existing operator (none of ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’). For example, the string ‘XXX’ does the job and causes WP to not enter the cases during the query generation and therefore also the 0=1 concatenation is skipped.
(The cases are located in the function
get_sql_for_clause()
of the file class-wp-tax-query.php, where the allowed operators are checked via conditions and there is no ‘else’.).
Firstly, don’t use
query_posts
. Create a newWP_Query
object instead.Secondly, why would you need to add all the categories, if you are literally adding ALL categories? Would this not be the same as getting all posts? You can also specify which to NOT include. ie. “Everything but Chicago”.