dongmangzong8006 2016-06-02 13:54
浏览 7

Wordpress SQL查询无法按预期工作

I have term parent called 'product_parent' that has three childs terms :

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

each post has **meta_key price ** (string) and i have to filter posts by price:

0 to 50, 50 to 100 and 100 +

this query works well (i can get 15 products):

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.

Someone could clarify me please? i don't want to use Wp_Query(), i just want to use sql.

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • duanliao3826 2016-06-02 14:20
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器