In a custom post type (products) I want to order products by summing multiple values based on user selection. I use custom taxonomies to filter the products.
I could use the orderby meta_value
but the combinations a user could make are so many that it's not possible to store all these totals as post meta.
I think I will need to alter the wp query using filters like posts_clauses
. Could a custom SQL query handle this effective (or maybe a different approach)?
It's a custom setup so I don't mind how the values are stored. Could be as post meta, in a custom table or even as a json/string in the post_content
field (I don't make use of it).
In a custom post type (products) I want to order products by summing multiple values based on user selection. I use custom taxonomies to filter the products.
I could use the orderby meta_value
but the combinations a user could make are so many that it's not possible to store all these totals as post meta.
I think I will need to alter the wp query using filters like posts_clauses
. Could a custom SQL query handle this effective (or maybe a different approach)?
It's a custom setup so I don't mind how the values are stored. Could be as post meta, in a custom table or even as a json/string in the post_content
field (I don't make use of it).
I wouldn't use taxonomies or custom fields for this purpose. Every time you're add one custom field for a query, you have to add a JOIN part for that query - it means that with 10-15 custom fields, you'll get query with 10-15 joins - so such query will be very slow.
But there is some nice trick you can use in such case. I would approach it this way:
post_id
which will allow you to join this table with posts table and then one column for every property - so basic_cost
, extra_cost
, extra_cost_2
and so on - or whatever your properties are...) This way you'll be able to use these columns in a sum and the query will need only one join.save_post
hook to fill that table with meta values.posts_clauses
hook to join your custom table and order by whatever sum you want based on fields in that table.
pre_get_posts
with URL parameters to filter the products. Products could be 1000 or 10.000+. Each filter (taxonomy) will correspond to a different value/price for each product. Simple I am looking for an approach to order by the total of multiple prices. – Christopher Commented Jan 27, 2019 at 10:24