【问题标题】:Wordpress: Dynamic query for taxonomiesWordpress:分类法的动态查询
【发布时间】:2012-06-13 06:29:56
【问题描述】:

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

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

例如:

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

如果 $city 为空,查询应该是这样的:

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

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

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

我该怎么做?

谢谢。

【问题讨论】:

    标签: wordpress search taxonomy


    【解决方案1】:

    我会这样做:

    $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,
        )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多