【问题标题】:how to filter the Wordpress loop from multiple form inputs using WP_query如何使用 WP_query 从多个表单输入中过滤 Wordpress 循环
【发布时间】:2012-11-16 11:41:00
【问题描述】:

我正在尝试使用一系列下拉框过滤循环。我的一切都设置好了,但我坚持使用最佳/正确的方法来过滤表单值的任何组合上的循环。

我目前拥有的是带有 2 个下拉框的表单 - job_type 和 job_cat 通过 $_GET 发布值 - 填充 WP_query:

<?php 
if (isset($_GET['action'])){
$jobtype = $_GET['type'];
$jobcat = $_GET['cat'];
}

$args = array(
'tax_query' => array(
        'relation' => 'AND',
    array(
        'taxonomy' => 'job_type',
        'field' => 'term_id',
        'terms' => $jobtype
    ),
        array(
        'taxonomy' => 'job_cat',
        'field' => 'slug',
        'terms' => $jobcat
    )
)
);



// The Query

$the_query = new WP_Query($args);?>

使用这种方法,我可以过滤两个值的循环,但我希望能够过滤任何组合。我最终将在表单上有大约 5 个过滤器选项。

所以用户可以只搜索 Job Type 或 Job cat 或两者都搜索。

我希望这是有道理的......?提前谢谢...

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您在 $jobtype 变量中传递作业类型的 ID,请将“term_id”替换为“id”。如果您正在传递 slug,则将其设为“slug”。 来源:http://codex.wordpress.org/Class_Reference/WP_Query

    【讨论】:

      猜你喜欢
      • 2013-04-16
      • 1970-01-01
      • 2018-08-08
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      相关资源
      最近更新 更多