I’m building a portfolio post type and I want to have a way to mark certain pieces as featured, some as normal (default) and others as archived. The pieces would only ever be one of these 3. Because of the singular value it makes sense to me that this be post meta. But, i’ve read that querying by taxonomy is supposedly faster, so I’ve built a solution where featured, normal and archived are 3 terms in a custom taxonomy.
Should I stick with the taxonomies for this case, or switch to post meta. I realize that is an opinion question, so to make it more specific: is the query speed improvements worth the added complexity of creating a custom taxonomy and modifying it so that it can only ever have 1 term? Is a taxonomy where you can only have 1 term defeating the purpose of a taxonomy in the first place?
I’m not sure that one can be called categorically faster than the other. And, in any case, it’s only really going to emerge when you start to scale up to many, many objects.
The issue is this: The postmeta table is a key-value table that is not indexed. If you have a certain number of rows in the table – say, a few hundred thousand – this can begin to cause speed issues, because MySQL has to do a full tablescan whenever you do a query like
...FROM $wpdb->posts WHERE meta_value = 'foo' and meta_value = 'bar'...
Taxonomy queries, on the other hand, require one or two subqueries. But the tables in question are aggressively indexed (and the queries are cacheable in a nice way) so that these queries perform dramatically better at scale (again, ‘scale’ means many, many thousands of posts).
As far as “complexity” is concerned – it’s really only a few extra lines of code for
register_taxonomy()
. Thetax_query
param forWP_Query
is no more complex thanmeta_query
, and the syntax ofwp_x_post_terms()
is only a bit more complex thanx_post_meta()
. And I wouldn’t worry about “defeating the purpose of a taxonomy”. The tools are there for you to use however it works best for your project 🙂 And there may come a day when, for some reason, you actually want to have multiple terms per item, in which case the choice of taxonomies would have been a forward-thinking choice.Is a taxonomy where you can only have 1 term defeating the purpose of a taxonomy in the first place?
You can change the way it appears in post edit page using metabox to make taxonomy select box, so that only one term is ever selected….
I think in your case meta query will be more faster than tax query as meta query requires 2 tables to join posts and postmeta, while taxonomy will require three tables posts,taxonomyrelation table and terms table….