wordpress 通过SQL获取前端可见的woocommerce产品(不私有,不受密码保护,能够在搜索或目录中显示)

rta7y2nd  于 2023-04-05  发布在  WordPress
关注(0)|答案(2)|浏览(235)

我正在运行以下SQL代码:

SELECT DISTINCT p.ID
                    FROM {$wpdb->prefix}posts AS p
                    INNER JOIN {$wpdb->prefix}term_relationships AS tr ON p.ID = tr.object_id
                    INNER JOIN {$wpdb->prefix}term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                    INNER JOIN {$wpdb->prefix}term_relationships AS tr2 ON p.ID = tr2.object_id
                    INNER JOIN {$wpdb->prefix}term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
                    INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
                    WHERE p.post_type = 'product'
                    AND p.post_status = 'publish'
                    AND tt.taxonomy = 'product_cat'
                    AND tt.term_id IN (%s)
                    AND tt.parent != 0
                    AND tt2.taxonomy = 'product_visibility'
                    AND tt2.term_id NOT IN ('" . wc_get_product_visibility_term_ids() . "')
                    AND pm.meta_key = '_stock_status'
                    AND pm.meta_value = 'instock'
                    GROUP BY p.ID
                    ORDER BY SUM(pm.meta_value) DESC, RAND()
                    LIMIT %d

每当我删除这个:

AND tt2.taxonomy = 'product_visibility'
                    AND tt2.term_id NOT IN ('" . wc_get_product_visibility_term_ids() . "')

然后,代码返回产品,但与它,它返回什么(甚至没有可见的产品)。我想只包括产品,应该是可见的前端以任何方式(不私有,不受密码保护,能够显示在搜索或目录)。

kh212irz

kh212irz1#

如果您尝试使用wc_get_product_visibility_term_ids()根据产品的可见性过滤产品,它将返回产品可见性分类的术语ID。问题可能是您使用的NOT IN需要一个值列表,而不是字符串,因此您可能需要传递一个值数组。

SELECT DISTINCT p.ID
FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}term_relationships AS tr ON p.ID = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}term_relationships AS tr2 ON p.ID = tr2.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND tt.taxonomy = 'product_cat'
AND tt.term_id IN (%s)
AND tt.parent != 0
AND tt2.taxonomy = 'product_visibility'
AND tt2.term_id NOT IN (" . implode(',', wc_get_product_visibility_term_ids()) . ")
AND pm.meta_key = '_stock_status'
AND pm.meta_value = 'instock'
GROUP BY p.ID
ORDER BY SUM(pm.meta_value) DESC, RAND()
LIMIT %d
  • wc_get_product_visibility_term_ids()周围的"'"替换为圆括号() +使用implode()将返回的术语ID数组转换为逗号分隔的值列表(应正确地从查询中排除产品可见性术语,并仅返回可见产品)
jutyujz0

jutyujz02#

我想明白了。可见产品并不总是有product_visibility条目。因此,我必须检查product_visibility是否为null,或者如果匹配,是否不是product_visibility ID使产品不可见。下面是更新的代码:

SELECT DISTINCT p.ID
                    FROM {$wpdb->prefix}posts AS p
                    INNER JOIN {$wpdb->prefix}term_relationships AS tr ON p.ID = tr.object_id
                    INNER JOIN {$wpdb->prefix}term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                    LEFT JOIN (
                      SELECT tr2.object_id
                      FROM {$wpdb->prefix}term_relationships AS tr2
                      INNER JOIN {$wpdb->prefix}term_taxonomy AS tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
                      WHERE tt2.taxonomy = 'product_visibility'
                      AND tt2.term_id IN (7, 6, 9, 10, 11, 12)
                    ) AS excluded_visibility ON p.ID = excluded_visibility.object_id
                    INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
                    WHERE p.post_type = 'product'
                    AND p.post_status = 'publish'
                    AND tt.taxonomy = 'product_cat'
                    AND tt.term_id IN (%s)
                    AND tt.parent != 0
                    AND (excluded_visibility.object_id IS NULL OR tr.term_taxonomy_id NOT IN (excluded_visibility.object_id))
                    AND pm.meta_key = '_stock_status'
                    AND pm.meta_value = 'instock'
                    GROUP BY p.ID
                    ORDER BY SUM(pm.meta_value) DESC, RAND()
                    LIMIT %d

相关问题