I'm trying to query posts using WP_Query and meta_query parameters:
$args = array(
'post_type' => 'produkty',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'produkt_aktywny',
'value' => '1',
'compare' => '='),
array(
'key' => 'produkt_dostepnosc',
'value' => '1',
'compare' => '=')
)
);
$query = new WP_Query( $args );
What should I add to order the results by two other keys ('produkt_kategoria' and 'produkt_cena')? There's nothing about it in WP Codex, but this:
"Do you know how to sort the query if the meta_value is an array? Write it here :)"
According to this, I believe sorting by multiple meta values is possible.
I wanted to show posts from a custom post type. Order it by a custom field, and add a parameter where an other custom field was a certain value to show only a selection of posts.
eg. select custom_posts where custom_field_a=xxxx and order by custom_field_b
It took me a while to figure it out and when searching for it, I kept on stumbling on this post. So for all people having the same issue, here is the (simplified) code that finally did it.
$args = array(
'post_type' => 'my_custom_post_type',
'meta_key' => 'custom_field_b',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'custom_field_a',
'value' => get_post_meta(get_the_ID(),'other_field',true),
'compare' => '='
)
),
);
$loop2 = new WP_Query($args);
You see that this is a loop in loop and therefor the value is dynamical. Of course, this could be hard coded too.
Hope it helps someone!
I believe you have to use 'meta_key' in the main $arg array. You can order by that meta value, but I don't think there's a built in way to order by 2 meta values. You can order by a second field (here, title), just not a second meta value field (as far as I know).
$args = array(
'post_type' => 'produkty',
'meta_key' => 'produkt_kategoria',
'orderby' => 'meta_value title',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'produkt_aktywny',
'value' => '1',
'compare' => '='),
array(
'key' => 'produkt_dostepnosc',
'value' => '1',
'compare' => '=')
)
);
You may have to write some SQL. Scroll down on this page: http://wpquestions.com/question/show/id/1916
This might help also:
https://wordpress.stackexchange.com/questions/10941/how-do-you-use-orderby-with-meta-query-in-wordpress-3-1
<h3>Doing request</h3>
<?php
$query = new WP_Query($args);
unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] = $query;
?>
<br />
<br />
<h3>Request done but without sorting, all we need from that request is difficult mysql query $query->request. Next lets get this,modificate and do again</h3>
Principle is simple: explode and add mysql string which is needed<br />
<?php
//****************************************************************************************
/* THIS IS REQUEST WHICH I WRITE BY HANDS TO ATCHIVE THAT I NEED. VERY USEFULL TO DO THIS BEFO PROGRAMMING!
*
* SELECT wp_posts.ID FROM wp_posts
* INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
* INNER JOIN wp_postmeta featured ON (wp_posts.ID = featured.post_id)
* WHERE 1=1
* AND wp_posts.post_type = 'car'
* AND (wp_posts.post_status = 'publish')
* AND ( (wp_postmeta.meta_key = 'car_year'
* AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '1900' AND '2013') ) //DO NOT LOOK AT THIS, EXAMPLE
* AND ( (featured.meta_key = 'car_is_featured') )
* GROUP BY wp_posts.ID
* ORDER BY featured.meta_value ASC, wp_posts.post_modified_gmt DESC LIMIT 0, 20
*/
//step 1
$request = str_replace("SQL_CALC_FOUND_ROWS", "", $query->request); //remove unnecessary code
//step 2 - explode to insert mysql code for featured cars
$tmp_request_array1 = explode('WHERE 1=1', $request);
$tmp_request_array1[0].=" INNER JOIN $wpdb->postmeta featured ON ($wpdb->posts.ID = featured.post_id) ";
$request = $tmp_request_array1[0] . " WHERE 1=1 " . $tmp_request_array1[1]; //assemble request again
//step 3 - explode assembled request to order by meta values
$tmp_request_array1 = explode('ORDER BY', $request);
$tmp_request_array2 = explode($order, $tmp_request_array1[1]);
if ($orderby == 'post_modified_gmt') {
$request = $tmp_request_array1[0] . ' ORDER BY ' . "featured.meta_value DESC, $wpdb->posts.post_modified_gmt $order" . $tmp_request_array2[1];
} else {
$request = $tmp_request_array1[0] . ' ORDER BY ' . "featured.meta_value DESC, $wpdb->postmeta.meta_value $order" . $tmp_request_array2[1];
}
//step 4 - explode assembled request again to say that featured.meta_key is = 'car_is_featured'
$tmp_request_array1 = explode('GROUP BY', $request);
$request = $tmp_request_array1[0] . " AND ( (featured.meta_key = 'car_is_featured') ) " . " GROUP BY " . $tmp_request_array1[1];
//**********************************************************
//step 5 - DO OUR NEW MYSQL REQUEST, AND GET CARS ORDER WE NEED
$request_result = array();
$tmp = $wpdb->get_results($request, ARRAY_N);
foreach ($tmp as $value) {
$request_result[] = $value[0];
}
//***
?>
<br />
<br />
Full article read here