Does tax_query really beats meta_query in all situations?

Let me start saying that I know a ‘tax_query’ is faster than ‘meta_query’.

It’s always recommended to use taxonomies over custom meta when filtering by custom data is required, but here I found myself in this situation.

I’m creating a Real Estate application where I have this obvious post type ‘property’. Each property is supposed to have X number of rooms and bathrooms.

Then in my app you can filter properties by range… for example:
Show all properties that have from 3 to 18 rooms.

The custom field way would be to add a custom field, eg: ‘rooms’ and then do my query like:

$args = array(
    'post_type'  => 'property',
    'meta_query' => array(
        'key'     => 'rooms',
        'value'   => array(3,18),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
    ),
);
$query = new WP_Query( $args );

The taxonomy way would be for example to create a custom taxonomy called ‘property_rooms’, then create the terms like ‘1’, ‘2’, ‘3’, etc.. and do my query by term ID’s like:

$args = array(
    'post_type'  => 'property',
    'tax_query' => array(
        'taxonomy' => 'property_rooms',
        'terms'      => array(3,4,5,6,7,8,9,10,11,12,13,14,15...etc) // Just pretend these are the corresponding term id's
    )
);
$query = new WP_Query( $args );

And here’s the question:
Does tax_query beats meta_query even in situations like this?

It may be relevant to mention that we could be talking about thousands of properties.

Keep in mind sorting by these fields is neither a requirement nor a consideration to trade the speed of the query.

2 s
2

Does tax_query beats meta_query even in situations like this?

No.

Taxonomies are appropriate if you have a common set of values that are shared by many posts, and you’re doing a simple comparison based on whether the post has or does not have a particular value. They are not appropriate if you need to perform numerical or range comparisons, like your example, or if the values are going to be unique for every post.

Here’s some examples of what should and should not be a taxonomy on a Real Estate site:

  • Property type eg. House/Apartment/Townhouse, should be a taxonomy.
  • Buy/Rent should be a taxonomy.
  • Features eg. Pets allowed/Air conditioning/Dishwasher, should be a taxonomy.
  • No. of bedrooms, should be meta.
  • No. of parking spaces, should be meta.
  • Address, should be meta.

On top of being faster to query, having taxonomies for those properties makes managing them easier in the back-end, and makes generating a list for filtering considerably easier and faster.

However, keep in mind that even if meta is the only viable option for filtering a value, using too many meta queries will really hurt the performance of the query. This is because you are querying based on the value of the meta, rather than an ID, and the value column is not indexed. However, just because it’s not indexed doesn’t mean it would be appropriate to index it yourself. This is because the meta_value column of wp_postmeta holds vastly different kinds and amounts of data for different things.

If you have a large number of numerical fields that you need to use for filtering, neither post meta nor taxonomies might be appropriate. You might be better off creating a custom table with your own indexes and more appropriate column types. You could then store those values in your custom table, and use the posts_clauses, or posts_join, and posts_where filters to add your own SQL to WP_Query to use your table for more efficient filtering.

Leave a Comment