如何使用串联将TAX_QUERY添加到$args

时间:2020-03-06 作者:parvez noor

我试图有条件地将参数馈送到wordpress循环中,因此希望根据过滤器系统提供的标记、类别或属性添加到参数中。

然而,我似乎不知道如何使用$arg将tax\\u查询添加到参数中=

例如,我想更改以下代码:

  if ($value[\'priceRange\'] == 0 && $value[\'tags\'] == 0 && !$value[\'type\']) {
    if(!$value[\'priceRange\']) {
      $value[\'priceRange\'] = array(0,1000000);
    }

    $args = array(
     \'post_type\' => \'product\',
     \'posts_per_page\' => -1,
    );
  }



  if ($value[\'priceRange\'] == 0 && $value[\'tags\'] != 0 && !$value[\'type\']) {
    $args = array(
      \'post_type\' => \'product\',
      \'posts_per_page\' => -1,
      \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
          \'taxonomy\' => \'product_tag\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
        array(
          \'taxonomy\' => \'product_cat\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
        array(
          \'taxonomy\' => \'pa_branding\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
      ),
    );
  }
为此:

  if ($value[\'priceRange\'] == 0 && $value[\'tags\'] == 0 && !$value[\'type\']) {
    if(!$value[\'priceRange\']) {
      $value[\'priceRange\'] = array(0,1000000);
    }

    $args = array(
     \'post_type\' => \'product\',
     \'posts_per_page\' => -1,
    );
  }



  if ($value[\'priceRange\'] == 0 && $value[\'tags\'] != 0 && !$value[\'type\']) {
    $args .= 
      \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
          \'taxonomy\' => \'product_tag\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
        array(
          \'taxonomy\' => \'product_cat\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
        array(
          \'taxonomy\' => \'pa_branding\',
          \'field\' => \'slug\',
          \'terms\' => $value[\'tags\'],
        ),
      ),
    );
  }
稍后,我还计划在此基础上添加一个针对价格的条件meta\\u查询。

有人有什么建议吗?

提前感谢!

1 个回复
SO网友:Daniel Gross

Try this...

<?php
if ( $value[ \'priceRange\' ] == 0 && $value[ \'tags\' ] == 0 && !$value[ \'type\' ] ) {
    if ( !$value[ \'priceRange\' ] ) {
        $value[ \'priceRange\' ] = array( 0, 1000000 );
    }

    $args = array(
        \'post_type\' => \'product\',
        \'posts_per_page\' => -1,
    );
}


if ( $value[ \'priceRange\' ] == 0 && $value[ \'tags\' ] != 0 && !$value[ \'type\' ] ) {

    $args[\'tax_query\'] = array(
        \'relation\' => \'OR\',
          array(
            \'taxonomy\' => \'product_tag\',
            \'field\' => \'slug\',
            \'terms\' => $value[ \'tags\' ],
          ),
          array(
            \'taxonomy\' => \'product_cat\',
            \'field\' => \'slug\',
            \'terms\' => $value[ \'tags\' ],
          ),
          array(
            \'taxonomy\' => \'pa_branding\',
            \'field\' => \'slug\',
            \'terms\' => $value[ \'tags\' ],
          ),
    );

}
?>

相关推荐

Skip latest 3 posts from loop

我下载了一个初学者主题(下划线)。我想从索引页的循环中排除前3篇文章。这是循环;<?php if ( have_posts() ) : if ( is_home() && ! is_front_page() ) : ?> <header> <h1 class=\"page-title screen-reader-text\"><?php sing