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.
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.
Share Improve this question edited Aug 7, 2019 at 8:58 Luis Rivera asked Aug 7, 2019 at 7:32 Luis RiveraLuis Rivera 3113 silver badges11 bronze badges 02 Answers
Reset to default 6Does 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.
Well, I wouldn't say that "tax_query beats meta_query"... They're a little bit different things.
When you perform tax_query, then two JOINS are needed in your SQL query. Meta_query results in only one join (if you search for only one meta).
So when is the tax_query better? For example when you search for posts assigned to multiple taxonomies.
Let's say you want to find properties that have 2 floors, 4 bedrooms, separate kitchen, and a garage. So you're doing a search with multiple conditions based on multiple properties.
In such case:
- tax_query will result in an SQL query with just 2 JOINS and multiple conditions in WHERE part.
- meta_query will result in an SQL query with 1 JOIN for every property in your search.
And even worse - tax_query uses proper IDs and keys and in meta_query you'll have to use strings as keys. So yeah - in such case tax_query has to be faster.
But... Tax_queries are really bad choice if:
- you need to order by its value (i.e. order properties by their price),
- you search for all values within given range (i.e. search for properties within given price range),
- you have a lot of different values for given property (i.e. price since there will be almost the same number of different prices as properties).
So no - tax_query does not beat meta_query every time. They're different things and you should use them according to your needs.