WP_Query - several custom message types and sorting by custom meta

I would like to get the results from two custom message types and sort them by custom meta (event trigger date).

This code:

$warsztaty_q = new WP_Query(array( 'post_type' => array('kalendarium', 'warsztaty'), 'order_by' => 'meta_value', 'meta_key' => 'data_start', 'order' => 'ASC' )); 

It would be nice, but first sorted "kalendarium", and then "warsztaty", so in the query results, messages from "kalendarium" are first sorted, and then "warsztaty" is not together.

I found this solution: https://wordpress.stackexchange.com/questions/71576/combining-queries-with-different-arguments-per-post-type

But I can not get the usual meta :(

Can anyone help me? :)

+6
source share
1 answer

use orderby instead of order_by

 $warsztaty_q = new WP_Query(array( 'post_type' => array('kalendarium', 'warsztaty'), 'orderby' => 'meta_value', 'meta_key' => 'data_start', 'order' => 'ASC' )); 

refer to codex for more details

+12
source

Source: https://habr.com/ru/post/953245/


All Articles