is it possible to add two meta_key's
in pre_get_posts
?
my current query
$query->set('s', '' );
$query->set( 'meta_key', 'cat_adresse_stadtteil' );
$query->set( 'meta_value', array('charlottenburg', 'wilmersdorf', 'schmargendorf') );
add this
$query->set('orderby','meta_value_num');
$query->set('meta_key', 'rank');
$query->set('order', 'ASC');
EDIT
Ok, i found this solution (link #example 2)
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'location',
'value' => 'Melbourne',
'compare' => '='
),
array(
'key' => 'attendees',
'value' => 100,
'type' => 'NUMERIC',
'compare' => '>'
)
)
);
but it doesnt work, any ideas what is wrong?
$query->set('meta_query',array(
array( 'key' => 'cat_adresse_stadtteil',
'value' => array('charlottenburg', 'wilmersdorf', 'schmargendorf'), ),
array( 'key' => 'rank'
'orderby' => 'meta_value_num',
'order' => 'ASC' ) ) );
To combine the two parts, you can try the following:
It looks like you were missing the
compare
parameter and using theorder
andorderby
parameters in a wrong place. I’m not sure though why you are resetting the search parameter.The generated SQL query will look something like:
You can convert this query to pre_pget_posts:
And compare param details: