WP_QUERY用于具有多个分类和术语的筛选器

时间:2018-09-01 作者:atempel

所以,我想做的是制作一个ajax过滤器,这是过滤器代码的一部分,它基于来自过滤器表单的数据构建$args,然后(不是在这段代码中)它给出查询的结果。

我有3个自定义分类法(及其表单功能):
-type\\u emp(一个选项来自表单)
-status\\u emp(一个或多个选项来自表单)
-city\\u emp(一个选项来自表单)

问题是:I\'m not able to use them in the WP_Query as filters, since they will match the post even if it doesn\'t have all the terms.

例如,type\\u emp为1,status\\u emp为null,city\\u emp为2

它将返回帖子:

(type\\u emp/status\\u emp/city\\u emp)
帖子1(1/2/3)
帖子2(2/1,4/2)
帖子3(1/4/2)

如您所见,post 3应该是唯一返回的,因为他拥有所有参数。Is there a way to restrict the query like that?
此外,我需要多次匹配status\\u emp,有时需要多个术语,在代码中,我分解字符串以生成一个数组,并将其作为术语放入税务查询中。How to I match posts that have at least one of them?

$args = array(
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'post_status\' => \'publish\'
);

if ( isset( $_POST[\'typefilter\'] ) && $_POST[\'typefilter\'] !== \'\' ) {
    $args[\'post_type\'] = \'post_type_x\';
    $args[\'tax_query\'] = array(
        array(
            \'taxonomy\' => \'type_emp\',
            \'field\' => \'id\',
            \'terms\' => $_POST[\'typefilter\']
        )
    );
}

if ( $_POST[\'typefilter\'] == \'\') {
    $args[\'post_type\'] = \'post_type_x\';
}

// If two or more options are selected they are sorted below
$query  = explode(\'&\', file_get_contents("php://input"));
$params = array();

foreach( $query as $param )
{
  list($name, $value) = explode(\'=\', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}

if( isset( $params[\'statusfilter\'] ) && $_POST[\'statusfilter\'] !== \'\' ) {
    $args[\'tax_query\'] .= array(
        array(
            \'taxonomy\' => \'status_emp\',
            \'field\' => \'id\',
            \'terms\' => $params[\'statusfilter\']
        )
    );
}

// for taxonomies / categories
if( isset( $_POST[\'cityfilter\'] ) && $_POST[\'cityfilter\'] !== \'\') {
    $args[\'tax_query\'] .= array(
        array(
            \'taxonomy\' => \'city_emp\',
            \'field\' => \'id\',
            \'terms\' => $_POST[\'cityfilter\']
        )
    );
}
$query = new WP_Query($args);
很抱歉,如果我的问题有点混乱,但我尽量说清楚。

1 个回复
最合适的回答,由SO网友:benny-ben 整理而成

请尝试以下方法:

$args = array(
    \'orderby\'     => \'date\',
    \'order\'       => \'DESC\',
    \'post_status\' => \'publish\'
);

if ( isset( $_POST[\'typefilter\'] ) && $_POST[\'typefilter\'] !== \'\' ) {
    $args[\'post_type\']                = \'post_type_x\';
    $args[\'tax_query\'][0][\'taxonomy\'] = \'type_emp\';
    $args[\'tax_query\'][0][\'field\']    = \'id\';
    $args[\'tax_query\'][0][\'terms\']    = $_POST[\'typefilter\'];
    $args[\'tax_query\'][0][\'operator\'] = \'IN\';
}

if ( $_POST[\'typefilter\'] == \'\') {
    $args[\'post_type\'] = \'post_type_x\';
}

// If two or more options are selected they are sorted below
$query  = explode(\'&\', file_get_contents("php://input"));
$params = array();

foreach( $query as $param )
{
  list($name, $value) = explode(\'=\', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}

if( isset( $params[\'statusfilter\'] ) && $_POST[\'statusfilter\'] !== \'\' ) {
    $args[\'tax_query\'][1][\'taxonomy\'] = \'status_emp\';
    $args[\'tax_query\'][1][\'field\']    = \'id\';
    $args[\'tax_query\'][1][\'terms\']    = $params[\'statusfilter\'];
    $args[\'tax_query\'][1][\'operator\'] = \'IN\';
}

// for taxonomies / categories
if( isset( $_POST[\'cityfilter\'] ) && $_POST[\'cityfilter\'] !== \'\') {
    $args[\'tax_query\'][2][\'taxonomy\'] = \'city_emp\';
    $args[\'tax_query\'][2][\'field\']    = \'id\';
    $args[\'tax_query\'][2][\'terms\']    = $_POST[\'cityfilter\'];
    $args[\'tax_query\'][2][\'operator\'] = \'IN\';
}

$query = new WP_Query($args);
我还没有测试过它,但理论上它应该可以工作:),告诉我它是否可以工作,或者你是否需要更多的帮助

结束
WP_QUERY用于具有多个分类和术语的筛选器 - 小码农CODE - 行之有效找到问题解决它

WP_QUERY用于具有多个分类和术语的筛选器

时间:2018-09-01 作者:atempel

所以,我想做的是制作一个ajax过滤器,这是过滤器代码的一部分,它基于来自过滤器表单的数据构建$args,然后(不是在这段代码中)它给出查询的结果。

我有3个自定义分类法(及其表单功能):
-type\\u emp(一个选项来自表单)
-status\\u emp(一个或多个选项来自表单)
-city\\u emp(一个选项来自表单)

问题是:I\'m not able to use them in the WP_Query as filters, since they will match the post even if it doesn\'t have all the terms.

例如,type\\u emp为1,status\\u emp为null,city\\u emp为2

它将返回帖子:

(type\\u emp/status\\u emp/city\\u emp)
帖子1(1/2/3)
帖子2(2/1,4/2)
帖子3(1/4/2)

如您所见,post 3应该是唯一返回的,因为他拥有所有参数。Is there a way to restrict the query like that?
此外,我需要多次匹配status\\u emp,有时需要多个术语,在代码中,我分解字符串以生成一个数组,并将其作为术语放入税务查询中。How to I match posts that have at least one of them?

$args = array(
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'post_status\' => \'publish\'
);

if ( isset( $_POST[\'typefilter\'] ) && $_POST[\'typefilter\'] !== \'\' ) {
    $args[\'post_type\'] = \'post_type_x\';
    $args[\'tax_query\'] = array(
        array(
            \'taxonomy\' => \'type_emp\',
            \'field\' => \'id\',
            \'terms\' => $_POST[\'typefilter\']
        )
    );
}

if ( $_POST[\'typefilter\'] == \'\') {
    $args[\'post_type\'] = \'post_type_x\';
}

// If two or more options are selected they are sorted below
$query  = explode(\'&\', file_get_contents("php://input"));
$params = array();

foreach( $query as $param )
{
  list($name, $value) = explode(\'=\', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}

if( isset( $params[\'statusfilter\'] ) && $_POST[\'statusfilter\'] !== \'\' ) {
    $args[\'tax_query\'] .= array(
        array(
            \'taxonomy\' => \'status_emp\',
            \'field\' => \'id\',
            \'terms\' => $params[\'statusfilter\']
        )
    );
}

// for taxonomies / categories
if( isset( $_POST[\'cityfilter\'] ) && $_POST[\'cityfilter\'] !== \'\') {
    $args[\'tax_query\'] .= array(
        array(
            \'taxonomy\' => \'city_emp\',
            \'field\' => \'id\',
            \'terms\' => $_POST[\'cityfilter\']
        )
    );
}
$query = new WP_Query($args);
很抱歉,如果我的问题有点混乱,但我尽量说清楚。

1 个回复
最合适的回答,由SO网友:benny-ben 整理而成

请尝试以下方法:

$args = array(
    \'orderby\'     => \'date\',
    \'order\'       => \'DESC\',
    \'post_status\' => \'publish\'
);

if ( isset( $_POST[\'typefilter\'] ) && $_POST[\'typefilter\'] !== \'\' ) {
    $args[\'post_type\']                = \'post_type_x\';
    $args[\'tax_query\'][0][\'taxonomy\'] = \'type_emp\';
    $args[\'tax_query\'][0][\'field\']    = \'id\';
    $args[\'tax_query\'][0][\'terms\']    = $_POST[\'typefilter\'];
    $args[\'tax_query\'][0][\'operator\'] = \'IN\';
}

if ( $_POST[\'typefilter\'] == \'\') {
    $args[\'post_type\'] = \'post_type_x\';
}

// If two or more options are selected they are sorted below
$query  = explode(\'&\', file_get_contents("php://input"));
$params = array();

foreach( $query as $param )
{
  list($name, $value) = explode(\'=\', $param, 2);
  $params[urldecode($name)][] = urldecode($value);
}

if( isset( $params[\'statusfilter\'] ) && $_POST[\'statusfilter\'] !== \'\' ) {
    $args[\'tax_query\'][1][\'taxonomy\'] = \'status_emp\';
    $args[\'tax_query\'][1][\'field\']    = \'id\';
    $args[\'tax_query\'][1][\'terms\']    = $params[\'statusfilter\'];
    $args[\'tax_query\'][1][\'operator\'] = \'IN\';
}

// for taxonomies / categories
if( isset( $_POST[\'cityfilter\'] ) && $_POST[\'cityfilter\'] !== \'\') {
    $args[\'tax_query\'][2][\'taxonomy\'] = \'city_emp\';
    $args[\'tax_query\'][2][\'field\']    = \'id\';
    $args[\'tax_query\'][2][\'terms\']    = $_POST[\'cityfilter\'];
    $args[\'tax_query\'][2][\'operator\'] = \'IN\';
}

$query = new WP_Query($args);
我还没有测试过它,但理论上它应该可以工作:),告诉我它是否可以工作,或者你是否需要更多的帮助

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请