All in my case I want to get post result by Gender, Age, City etc…
I am using this query. If i have any of variable like age, city, gender for that “AND” operation will perform and get only result for that variable.
Note: Consider those case also where i have no variable like city but it should work for gender & state.
My Query is
$gender = $_GET['gender'];
$city = $_GET['city'];
$state = $_GET['state'];
$wp_query = new WP_Query( array(
'post_type' => 'profile',
'meta_query' => array(
'relation' => 'AND',
array('meta_key' => 'wpcf-gender', 'meta_value' => $_GET['gender'], 'meta_compare' => '=='),
array('meta_key' => 'wpcf-city', 'meta_value' => $_GET['city'], 'meta_compare' => '=='),
array('meta_key' => 'wpcf-state', 'meta_value' => $_GET['state'], 'meta_compare' => '==')
),
'paged' => $paged )
);
Here is a modification of your code where you can add the meta query if certain $GET keys are set.
Reference:
array_push()
: http://php.net/manual/en/function.array-push.phpshould be