【问题标题】:WP_Query Woocommerce products that belong in distinct multiple categories only tax_queryWP_Query Woocommerce 产品仅属于不同的多个类别 tax_query
【发布时间】:2013-12-23 02:10:24
【问题描述】:

我将WP_Query 用于 Woocommerce 产品,以尝试查询特定类别的产品。这是对我有用的语法 -

$args = array(
    'posts_per_page' => -1,
    'product_cat' => 'category-slug-here',
    'post_type' => 'product',
    'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '' . get_the_title() . '<br /><br />';
}
wp_reset_postdata();

这会返回数据,但我想传递一个 ID,而不是类别 slug,以进行过滤,并且我想查找存在于多个类别中的产品。

product_cat 的参数不是 WP_Query 的原生参数(至少我可以找到),所以我假设这是 Woocommerce 的自定义。通过他们的文档,我无法找到任何可以让我按类别 ID 进行过滤的内容,也无法使用 AND 条件进行此过滤。

使用cattax_querycategory__and 的数组没有产生任何结果。本质上,我想查询类别 ID 102 和 115 中存在的所有产品。如果我必须使用 slug,我确信有办法根据我拥有的 ID 获取该信息,但我会希望避免 2 个查询按多个类别进行过滤。

有谁知道如何做到这一点?

更新:我了解到在 product_cat 参数中用逗号分隔类别 slug 会产生“或”效果,因此它将结合两者的不同产品,但这不是我想要的我正在寻找。所以,例如:

 'product_cat' => 'category-slug1, category-slug2'

将总共返回两个类别的产品,但我仍在寻找一种方法来查找仅属于两个或多个类别的不同产品。

【问题讨论】:

    标签: wordpress categories woocommerce


    【解决方案1】:

    哇,经过几个小时的敲打,这就是我能够解决这个问题的方法 -

    $args = array(
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'category-slug1'
            ),
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'category-slug2'
            )
        ),
        'post_type' => 'product',
        'orderby' => 'title',
    );
    $the_query = new WP_Query( $args );
    

    这利用了tax_query 参数(包括relation =&gt; 'AND')来确保产品属于这两个类别。

    希望这对将来的某人有所帮助。

    我也无法弄清楚如何传递一个 ID,而不是一个 slug(虽然我确信有办法),但这是基于 ID 检索 slug 的函数:

    $terms = get_term($YOURID, 'product_cat'); 
    $theslug = $terms->slug; 
    

    【讨论】:

    • RCNeil 当您在 2 个类别中拥有相同的产品时,是这样吗?我注意到当我有一个属于 2 个类别的产品时,WooCommerce 只显示两个类别中 1 个类别的产品详细信息。试图弄清楚如何在sweetschocolate 中显示例如smarties 并拥有sweets/smartieschocolate/smarties 的url 路径,因为我的WooCommerce 只为两者显示一个url
    • 是的,此查询适用于属于 2 个(或更多)类别的产品。我不确定 Woocommerce 如何确定哪个类别优先的 URL。我在一家汽车配件商店使用它,大多数时候它根据品牌/型号/汽车/零件而不是品牌/零件制作 URL,但它会有所不同。
    • 所以这会改变每个类别中产品的 URL?我应该把它放在哪里?
    • 我的意思是,如果您将产品分为 2 个类别,那么将创建 2 个 URL,例如 sweets/smartieschocolates/smarties,并且两者都是正确的。两者都将解决该单一产品。当您查询它们时,我不知道它返回哪个永久链接,我不确定该优先级是如何确定的,但无论哪种方式都可以。我不知道你还问我什么。
    • 您将脚本放在 WooCommerce 中的什么位置?我可以尝试解决问题
    【解决方案2】:

    来自WP_Query for Category Parameters 上的 WordPress 法典:

    相当于OR

    $args = array( 'product_cat' => 'category-slug1,category-slug2' ) );
    

    相当于AND

    $args = array( 'product_cat' => 'category-slug1+category-slug2' );
    

    例如

    $query = new WP_Query( $args );
    

    【讨论】:

      【解决方案3】:

      按 category_ID 查询这对我有用。

      // First obtain term id:
      //...
      $all_categories = get_categories( $args );
      $cat_ids = array();
      
      foreach ($all_categories as $cat) 
      {
           array_push($cat_ids, $cat->term_id);
      }
      
      //Now use ids from array:
      $args = array(
          'posts_per_page' => -1,
          'post_type' => 'product',
          'tax_query'     => array(
              array(
                  'taxonomy'  => 'product_cat',
                  'field'     => 'id', 
                  'terms'     => $cat_ids
              )
          )
      );
      

      【讨论】:

      • 这会执行 'OR' 查询
      • 字段应该有值'term_id':'tax_query' =&gt; array(array('taxonomy' =&gt; 'product_cat', 'field' =&gt; 'term_id', 'terms' =&gt; $cat_ids))
      【解决方案4】:

      在“tax_query”数组的数组中,您可以指定要对查询执行的“运算符”。您可以使用“AND”运算符实现您想要的。

      $args = array(
      'posts_per_page' => -1,
      'tax_query' => array(
          'relation' => 'AND',
          array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => array( 'category-slug1', 'category-slug2' )
              'operator => 'AND',
          ),
      ),
      'post_type' => 'product',
      'orderby' => 'title',
      );
      $the_query = new WP_Query( $args );
      

      此查询选择的所有产品都将与提供的“条款”相匹配。有关更多信息,请参阅此链接:https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

      如果链接断开,请查看相关信息:

      operator (string) - 要测试的运算符。可能的值为“IN”、“NOT IN”、“AND”、“EXISTS”和“NOT EXISTS”。默认值为“IN”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-26
        • 2018-05-17
        • 2013-01-20
        • 2020-10-20
        • 2018-12-19
        • 1970-01-01
        • 2015-03-02
        • 1970-01-01
        相关资源
        最近更新 更多