In wp_query, is it possible to build a complex query so I can order by a conditional or calculated fields?
what I’m trying to do is something like the next query in MySql:
SELECT *, field1, field2
case when field1+field2 > some_value then 1 else 2 end as my_alias
FROM my_table
ORDER BY my_alias ASC
I want to build queries like this one using wp_query, is this possible? if yes, how can I accomplish that?
Yeah, you need to
add custom field using
https://developer.wordpress.org/reference/hooks/posts_fields/
add custom order by using
https://developer.wordpress.org/reference/hooks/posts_orderby/
I don’t see any way to do this with a single
WP_Query
asmeta_query
doesn’t allow you such flexibility, though you can do three distinct queries then merge them (untested code):The thing is to use
meta_query
to compare the meta values with the actual date (you may want to change the date format depending of how they are stored indate_from
anddate_to
fields), and do a little loop right after to add a property to all post object with the rightevent_status
which you can work with when displaying posts.Maybe there is a clever way to achieve this through WP_Query filters but it would need more in-depth investigation inside
WP_Query
source code as it is not really documented inside the codex.I thing You use, and try this,
Look at this Click Here