Hello, some time ago:
- I had the regular
postand the custom
eventopost type had a
- the search result loop had to return a list of posts from both the post types (if relevant for the search), ordered by date;
postthe considered field when ordering had to be the regular
post_date, while for
eventoit had to be
An example to clarify better.
These posts' dates:
PT "post" PT "evento" ----------------------- ------------------------- post_date = 2019-10-10 ev_data_out = 2020-11-10 post_date = 2017-03-12 ev_data_out = 2018-12-01
had to return a list ordered this way:
Results -------------- 2020-11-10 2019-10-10 2018-12-01 2017-03-12
Since my knowledge of Wp Query was (and is) still basic but I needed a fast implementation, I came out with this:
- I hooked a function to
post_savetelling it to
update_post_metaa post meta called
order_datefor each post type, with
post_date's value if
ev_data_out's value if
- I filtered
pre_get_postswith a function that conditioned to
if (is_search && !is_admin())and setting
$query -> set('meta_key', 'order_date');and
$query -> set('orderby', 'meta_value');
So far I never had to retouch this stuff for it seems to work perfectly.
My question is, rather, if there is a method inherently better in Wordpress terms, i.e. if -with a better knowledge of the Wp Query, and of the meta queries in particular- I could get something more efficient and less DB-consuming.
N.b. I wish to understand things and not only to copy/paste, so an answer like "yes, study Wp Query better and you'll get the same result" would be perfectly fine for me!