1

我有一个搜索表单,其中包含几个“选择”,其中列出了分类的各种术语(“州”)。通过按下提交按钮,我将信息传递到构建查询的搜索结果页面。

问题是我需要动态构建查询,因为不必为每个“选择”选择值。所以有些值发送为空。

例如:

$country = $_POST["country"];
$city = $_POST["city"];

如果 $city 为空,则查询应如下所示:

$my_query = new WP_Query(array(
    'state' => $country
    )
);

但是如果 $country 和 $city 不为空,则 de 查询应该是这样的:

$my_query = new WP_Query(array(
    'state' => $country,
    'state' => $city
    )
);

我该怎么做?

谢谢。

4

1 回答 1

0

我会这样做:

$city = isset($_POST['city']) ? $_POST['city'] : null;
$country = isset($_POST['country']) ? (isset($_POST['city']) ? ' ('. $_POST['country'] .')' : $_POST['country']) : null;

$state = $city . $country;

此代码将显示:

  • 城市和国家:加拉加斯(委内瑞拉)
  • 唯一城市:加拉加斯
  • 唯一国家:委内瑞拉

你的查询:

$my_query = new WP_Query(array(
        'state' => $state,
    )
);
于 2012-06-23T01:44:48.370 回答