hi guys, I’m facing a strange problem with SQL in WP custom query. when I add the Left Joins specially these
LEFT JOIN wp_postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id
LEFT JOIN wp_postmeta wpostmeta3 ON wposts.ID = wpostmeta3.post_id
LEFT JOIN wp_postmeta wpostmeta4 ON wposts.ID = wpostmeta4.post_id
in the following query
SELECT distinct wposts.* FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id
LEFT JOIN wp_postmeta wpostmeta3 ON wposts.ID = wpostmeta3.post_id
LEFT JOIN wp_postmeta wpostmeta4 ON wposts.ID = wpostmeta4.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE (wposts.post_type = 'post' OR wposts.post_type = 'page' OR wposts.post_type = 'storhushall')
AND wposts.post_status = 'publish'
AND (wp_term_taxonomy.taxonomy = 'wb_business_recept_category' OR wp_term_taxonomy.taxonomy = 'wb_business_product_category'
OR post_parent=12
OR (wpostmeta2.meta_key = 'art_nr' AND wpostmeta2.meta_value LIKE '%7023%')
OR (wpostmeta3.meta_key = 'andra_versioner' AND wpostmeta3.meta_value LIKE '%7023%') OR (wpostmeta4.meta_key = 'viktpaket' AND wpostmeta4.meta_value LIKE '%7023%') OR (wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id IN (11,13))) group by wposts.ID
it halts and takes forever to load in MYSQL. and when I run it at front end. it gives Internal server error.
I don’t have PMA installed at my server. It is VisualMin which allows to run Execute SQL on server. So, Yes I tried and it goes forever to load.
I’m using joins in where clause, because I wanna search from custom fields too. Thanks
Ok, maybe since you’re using OR’s it might work. But I’m wondering why you have to join the same table four times? Wouldn’t once be enough? :
SELECT DISTINCT wposts.*
FROM wp_posts AS wposts
LEFT JOIN wp_postmeta AS wpostmeta
ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships
ON wposts.ID = wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE wposts.post_type IN ('post', 'page', 'storhushall')
AND wposts.post_status = 'publish'
AND ( wp_term_taxonomy.taxonomy = 'wb_business_recept_category'
OR wp_term_taxonomy.taxonomy = 'wb_business_product_category'
OR post_parent=12
OR (wpostmeta.meta_key = 'art_nr' AND wpostmeta.meta_value LIKE '%7023%')
OR (wpostmeta.meta_key = 'andra_versioner' AND wpostmeta.meta_value LIKE '%7023%')
OR (wpostmeta.meta_key = 'viktpaket' AND wpostmeta.meta_value LIKE '%7023%')
OR (wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id IN (11,13))
)
GROUP BY wposts.ID