我正在运行以下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() . "')
然后,代码返回产品,但与它,它返回什么(甚至没有可见的产品)。我想只包括产品,应该是可见的前端以任何方式(不私有,不受密码保护,能够显示在搜索或目录)。
2条答案
按热度按时间kh212irz1#
如果您尝试使用
wc_get_product_visibility_term_ids()
根据产品的可见性过滤产品,它将返回产品可见性分类的术语ID。问题可能是您使用的NOT IN
需要一个值列表,而不是字符串,因此您可能需要传递一个值数组。wc_get_product_visibility_term_ids()
周围的"'"
替换为圆括号()
+使用implode()
将返回的术语ID数组转换为逗号分隔的值列表(应正确地从查询中排除产品可见性术语,并仅返回可见产品)jutyujz02#
我想明白了。可见产品并不总是有product_visibility条目。因此,我必须检查product_visibility是否为null,或者如果匹配,是否不是product_visibility ID使产品不可见。下面是更新的代码: