简体   繁体   English

Wordpress SQL查询无法按预期工作

[英]Wordpress sql query not working as expected

I have term parent called 'product_parent' that has three childs terms : 我有一个名为“ product_parent”的父项,其中有三个子项:

  • child_term_1 (id=2) (5 posts) child_term_1(id = 2)(5个帖子)
  • child_term_2 (id=3) (3 posts) child_term_2(id = 3)(3个帖子)
  • child_term_3 (id=4) (10 posts) child_term_3(id = 4)(10个帖子)

each post has **meta_key price ** (string) and i have to filter posts by price: 每个帖子都有** meta_key价格**(字符串),我必须按价格过滤帖子:

0 to 50, 50 to 100 and 100 + 0至50、50至100和100 +

this query works well (i can get 15 products): 此查询效果很好(我可以获得15种产品):

SELECT DISTINCT p.ID FROM wp_posts as p 
LEFT JOIN wp_postmeta as p ON price.post_id = p.ID 
LEFT JOIN wp_relationships as t ON t.object_id = p.ID 
WHERE p.post_type = 'product' AND post_status = 'publish' 
AND t.term_taxonomy_id IN ('2', '3', '4') 
AND p.meta_key = 'price' 
AND p.meta_value LIKE '%50-100%' 
OR  p.meta_value LIKE '%100+%' 
GROUP BY p.ID

but if i add one filter : 但是如果我添加一个过滤器:

SELECT DISTINCT p.ID FROM wp_posts as p 
LEFT JOIN wp_postmeta as p ON price.post_id = p.ID 
LEFT JOIN wp_relationships as t ON t.object_id = p.ID 
WHERE p.post_type = 'product' AND post_status = 'publish' 
AND t.term_taxonomy_id IN ('2', '3', '4') 
AND p.meta_key = 'price' 
AND p.meta_value LIKE '%0-50%'
OR p.meta_value LIKE '%50-100%' 
OR  p.meta_value LIKE '%100+%' 
GROUP BY p.ID

This returns "many posts" that not in term_taxonomy_id specified. 这将返回未在term_taxonomy_id中指定的“许多帖子”。

Someone could clarify me please? 有人可以澄清一下吗? i don't want to use Wp_Query(), i just want to use sql. 我不想使用Wp_Query(),我只想使用sql。

Thanks for your help. 谢谢你的帮助。

you use the same alias 您使用相同的别名

then duplicate alias 然后重复别名

try: 尝试:

SELECT DISTINCT p.ID FROM wp_posts as p 
LEFT JOIN wp_postmeta as pm ON pm.post_id = p.ID 
LEFT JOIN wp_relationships as r ON r.object_id = p.ID 
WHERE p.post_type = 'product' AND p.post_status = 'publish' 
AND r.term_taxonomy_id IN ('2', '3', '4') 
AND pm.meta_key = 'price' 
AND pm.meta_value LIKE '%0-50%'
OR pm.meta_value LIKE '%50-100%' 
OR  pm.meta_value LIKE '%100+%' 
GROUP BY p.ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM