I have a search form that has 3 taxonomy fields and one post meta field. The form works well when I use taxonomy fields. But when I use the post meta field, all posts are displayed.
Where is the problem with my code?
add_filter('query_vars', 'register_query_vars');
function register_query_vars($vars)
{
$vars[] .= 'state';
$vars[] .= 'city';
$vars[] .= 'loc';
$vars[] .= 'name';
$vars[] .= 'status';
return $vars;
}
add_action('pre_get_posts', 'main_search_query');
function main_search_query($query)
{
if ($query->is_post_type_archive('estate') && $query->is_main_query() && !is_admin()) {
$name = get_query_var('name', FALSE);
$status = get_query_var('status', FALSE);
$state = get_query_var('state', FALSE);
$city = get_query_var('city', FALSE);
$loc = get_query_var('loc', FALSE);
$status_query = array('relation' => 'AND');
$status ? array_push($status_query, array('meta_query' => array( array( 'key' =>'_status', 'value' => $status, 'compare' => 'LIKE' )))) : null;
$query->set('meta_query', $status_query);
$tax_query = array('relation' => 'AND');
$allofcity = ($city == 0) ? array('taxonomy' => 'add_prm', 'field' => 'temr_id', 'terms' => $state) : array('taxonomy' => 'add_prm', 'field' => 'term_id', 'parent' => $state, 'terms' => $city);
$state ? array_push($tax_query, $allofcity) : null;
$city ? array_push($tax_query, array('taxonomy' => 'add_prm', 'field' => 'term_id', 'terms' => $city)) : null;
$loc ? array_push($tax_query, array('taxonomy' => 'add_cat', 'field' => 'term_id', 'terms' => $loc)) : null;
$name ? array_push($tax_query, array('taxonomy' => 'add_name', 'field' => 'term_id', 'terms' => $name)) : null;
$query->set('tax_query', $tax_query);
}
}