I have a custom post type “Listing” and I want to get all Listings that have a custom field gateway_value != 'Yes'
, and order the results by another custom field, location_level1_value
. I can get the queries to work separately, but I can’t combine them:
Query 1 (sort by location):
$wp_query = new WP_Query( array (
'post_type' => 'listing',
'post_status' => 'publish',
'posts_per_page' => '9',
'meta_key' => 'location_level1_value',
'orderby' => 'location_level1_value',
'order' => 'ASC',
'paged' => $paged
)
);
Query 2 (custom field value != Yes):
$wp_query = new WP_Query( array (
'post_type' => 'listing',
'posts_per_page' => '9',
'post_status' => 'publish',
'meta_key' => 'gateway_value',
'meta_value' => 'Yes',
'meta_compare' => '!=',
'paged' => $paged
)
);
Combined query:
I looked at the codex for help with this, but the following query does not work:
$wp_query = new WP_Query( array (
'post_type' => 'listing',
'posts_per_page' => '9',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'gateway_value',
'value' => 'Yes',
'compare' => '!='
),
array(
'key' => 'location_level1_value'
)
),
'orderby' => "location_level1_value",
'order' => 'ASC',
'paged' => $paged
)
);
What am I doing wrong with the combined query?
[UPDATE]: So now that 3.1 has been released, the combined query above still does not work. I do get results, just not sorted correctly.
[UPDATE]: var_dump($wp_query->request)
gives the following:
string(527) " SELECT SQL_CALC_FOUND_ROWS wp_7v1oev_posts.* FROM wp_7v1oev_posts
INNER JOIN wp_7v1oev_postmeta ON (wp_7v1oev_posts.ID = wp_7v1oev_postmeta.post_id)
INNER JOIN wp_7v1oev_postmeta AS mt1 ON (wp_7v1oev_posts.ID = mt1.post_id) WHERE 1=1 AND wp_7v1oev_posts.post_type="listing" AND (wp_7v1oev_posts.post_status="publish") AND wp_7v1oev_postmeta.meta_key = 'gateway_value' AND CAST(wp_7v1oev_postmeta.meta_value AS CHAR) != 'Yes' AND mt1.meta_key = 'location_level1_value' ORDER BY wp_7v1oev_posts.post_date DESC LIMIT 0, 9"
3 s
You could use the query to filter the content as you intended by using the ‘meta_query’ with filtering options, and for the order part, just add/modify the following parameters:
- ‘orderby’ => ‘meta_value’
- ‘meta_key’ => ‘location_level1_value’
-
‘order’ => ‘ASC’
$wp_query = new WP_Query( array ( 'post_type' => 'listing', 'posts_per_page' => '9', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'gateway_value', 'value' => 'Yes', 'compare' => '!=' ) ), 'orderby' => 'meta_value', // this means we will be using a selected // meta field to order 'meta_key' => 'location_level1_value', // this states which meta field // will be used in the ordering, // regardless of the filters 'order' => 'ASC', 'paged' => $paged ) );