Your query is completely wrong. You should use a proper tax_query
. Also note, your query is quite expensive to run to just count posts. Set the fields
paremeter to only return post ID’s. This saves up to 99.9% on resources.
Just a note, the field parameter inside a tax_query
is important. The valid values are slug
, term_id
and name
. This value must correspond with the value passed to the terms
parameter. So, if fields are set to slug
, you must pass the term slug to terms
. Do not use the name
parameter, there is a bug in the WP_Tax_Query
class where the query fails if a term’s name consists of more than one word.
You can try the following
$args = array(
'post_type' => 'accommodations',
'no_paging' => true, // Gets all posts
'fields' => 'ids', // Gets only the post ID's, saves on resources
'tax_query' => array(
array(
'taxonomy' => 'location', // Taxonomy name
'field' => 'slug', // Field to check, valid values are term_id, slug and name
'terms' => 'new-york' // This value must correspond to field value. If slug, use term slug
)
),
);
$the_query = new WP_Query( $args );
echo $the_query->found_posts;