Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP_Query - multiple custom post types and sort by custom meta

I'd like get results from two custom post types and sort them by custom meta (date of start events).

This code:

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

Would be ok but first sorted is 'kalendarium' and then 'warsztaty' so in query results first sorted are posts from 'kalendarium' and next from 'warsztaty' not together.

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

But I can't get custom meta :(

Anyone can help me? :)

like image 430
martin_682 Avatar asked Sep 05 '13 14:09

martin_682


1 Answers

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 codex for more details

like image 110
Jothi Kannan Avatar answered Sep 23 '22 13:09

Jothi Kannan