如何检查该类别是否具有产品或空的WordPress?

发布于 2025-02-03 00:37:20 字数 791 浏览 4 评论 0原文

大家好,我想知道具有某些自定义属性的特定类别是否具有产品,但我不知道如何使用此功能检查它是否具有

 $isset_products_in_category = new WP_Query(
      array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'relation'=>'AND',
        'tax_query'           => array(
          array(
            'taxonomy'        => 'product_cat',
            'field'           => 'term_id',
            'terms'           => '5',
            'operator'        => 'IN',
          ),
          array(
            'taxonomy'        => 'pa_color',
            'field'           => 'term_id',
            'operator'        => 'IN',
            'terms' => '2',
          ),
        ),
        'ignore_stickie_posts' => true,
        'fields' => 'ids',
      )
    );

hello guys i'm trying to know if a specific category with some custom attributes has a product or not but i don't know how to use this function to check if it has or no

 $isset_products_in_category = new WP_Query(
      array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'relation'=>'AND',
        'tax_query'           => array(
          array(
            'taxonomy'        => 'product_cat',
            'field'           => 'term_id',
            'terms'           => '5',
            'operator'        => 'IN',
          ),
          array(
            'taxonomy'        => 'pa_color',
            'field'           => 'term_id',
            'operator'        => 'IN',
            'terms' => '2',
          ),
        ),
        'ignore_stickie_posts' => true,
        'fields' => 'ids',
      )
    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2025-02-10 00:37:20
  1. '关系'=> '和'应该是sax_query wp_query()中的数组的一部分(它定义 sax_query 中的阵列之间的关系。
  2. 您没有在查询的结果上做任何事情。您可以检查计数:

修订的代码示例:

<?php 
$wp_query = new WP_Query(
  array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
      'relation'=>'AND',
      array(
        'taxonomy'        => 'product_cat',
        'field'           => 'term_id',
        'terms'           => '5',
        'operator'        => 'IN',
      ),
      array(
        'taxonomy'        => 'pa_color',
        'field'           => 'term_id',
        'terms'           => '2',
        'operator'        => 'IN',
      ),
    ),
    'ignore_sticky_posts' => true,
    'fields' => 'ids',
  )
);

// Do something with the result of the query
$isset_products_in_category = $wp_query->post_count > 0; 

var_dump($isset_products_in_category); // will output bool(true) or bool(false).
  1. 'relation' => 'AND' should be part of the tax_query array in WP_Query() (it defines the relationship between the arrays in the tax_query).
  2. Your not doing anything with the result of the query. You could check the count:

Revised code example:

<?php 
$wp_query = new WP_Query(
  array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
      'relation'=>'AND',
      array(
        'taxonomy'        => 'product_cat',
        'field'           => 'term_id',
        'terms'           => '5',
        'operator'        => 'IN',
      ),
      array(
        'taxonomy'        => 'pa_color',
        'field'           => 'term_id',
        'terms'           => '2',
        'operator'        => 'IN',
      ),
    ),
    'ignore_sticky_posts' => true,
    'fields' => 'ids',
  )
);

// Do something with the result of the query
$isset_products_in_category = $wp_query->post_count > 0; 

var_dump($isset_products_in_category); // will output bool(true) or bool(false).
变身佩奇 2025-02-10 00:37:20

您的WP_QUERY代码中有两个关键问题

i)关系不正确的位置

'关系'=&gt; “和”必须放置在“ sax_query”阵列内,而不是顶层。

2)参数键中的错字

'ignore_stickie_posts'是一个错字;应该是“ image_sticky_posts”。
“关系”必须在tax_query下移动。

校正的代码下面

$isset_products = new WP_Query(
array(
    'post_type'      => 'product',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => true,
    'fields'         => 'ids',
    'tax_query'      => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => 5,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'pa_color',
            'field'    => 'term_id',
            'terms'    => 2,
            'operator' => 'IN',
        ),
    ),
));

,然后检查该类别是否具有以下产品或空的产品,

if ($isset_products->have_posts()): { echo 'somthing'; } else{ echo 'Not found'; }

不要忘记

wp_reset_postdata();

There are two key issues in your WP_Query code

i) Incorrect Placement of relation

The 'relation' => 'AND' must be placed inside the 'tax_query' array, not at the top level.

2) Typos in Argument Keys

'ignore_stickie_posts' is a typo; it should be 'ignore_sticky_posts'.
'relation' must be moved under tax_query.

Corrected Codes below

$isset_products = new WP_Query(
array(
    'post_type'      => 'product',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => true,
    'fields'         => 'ids',
    'tax_query'      => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => 5,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'pa_color',
            'field'    => 'term_id',
            'terms'    => 2,
            'operator' => 'IN',
        ),
    ),
));

And then check if the category has products or empty like below

if ($isset_products->have_posts()): { echo 'somthing'; } else{ echo 'Not found'; }

Don't forget to

wp_reset_postdata();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文