Does tax_query really beats meta_query in all situations?

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