针对以下第一条评论更新了此答案:
if(!empty($_GET[\'city\']) AND !empty($_GET[\'state\']))
{ // if both fields are filled out, find houses that match both
$relation = "AND";
}
elseif(empty($_GET[\'city\']) OR empty($_GET[\'state\']))
{ // if either of the fields are empty, find houses that match either field
$relation = "OR";
}
$args = array(
\'post_type\' => \'house\',
\'post_title\' => $housename,
\'meta_query\' => array(
\'relation\' => $relation,// use $relation variable instead of string here
array(
\'key\' => \'house_city\',
\'value\' => $city
),
array(
\'key\' => \'house_state\',
\'value\' => $state
)
)
);
我不确定,但我认为如果$housename是空的,那么会寻找带有空帖子标题的房子。您可能需要有条件地将该部分添加到查询中,类似这样的操作可能会起作用:
if(!empty($_GET[\'housename\']))
{
$houseNameQuery = "\'post_title\' => $_GET[\'housename\']";
}
然后在$参数中替换“post\\u title”参数:
$args = array(
\'post_type\' => \'house\',
$houseNameQuery,
\'meta_query\' => array(...