我有一个带有自定义角色和自定义分类的站点。我正在进行高级搜索,自定义帖子可以通过角色和分类法进行筛选。
为了实现这一点,我使用了自己的自定义SQL查询,它可以很好地处理角色和一种分类法,但我很难将其用于第二种分类法(我没有得到任何结果)。下面是我正在生成的代码(一些示例值来自表单):
SELECT DISTINCT p.* FROM wp_posts p
LEFT JOIN wp_usermeta um ON p.post_author = um.user_id
LEFT JOIN wp_term_relationships txr ON p.ID = txr.object_id
LEFT JOIN wp_term_taxonomy tx ON txr.term_taxonomy_id = tx.term_taxonomy_id
LEFT JOIN wp_terms trm ON tx.term_id = trm.term_id
LEFT JOIN wp_terms trk ON tx.term_id = trk.term_id
WHERE (um.meta_key = \'wp_capabilities\' AND (um.meta_value LIKE \'%student%\' OR um.meta_value LIKE \'%instructor%\' ))
AND (tx.taxonomy= \'mediums\' AND ( trm.name LIKE \'%acrylic%\' OR trm.name LIKE \'%oil%\' ))
AND ( tx.taxonomy= \'keywords\' AND ( trk.name LIKE \'%landscape%\' OR trk.name LIKE \'%test%\'))
AND p.post_status = \'publish\'
AND p.post_type = \'gallery\'
GROUP BY p.ID
ORDER BY p.post_date DESC
当我将其直接输入到PHPMyAdmin中时,我没有收到任何错误,但它只是挂起(“加载消息”)。
我做错了什么?
SO网友:coopersita
我可以用这个查询解决我的问题,尽管我打赌它会更有效(我只是不知道怎么做)
SELECT DISTINCT p.* FROM wp_posts p
LEFT JOIN wp_usermeta um ON p.post_author = um.user_id
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
INNER JOIN (
SELECT txrm.object_id FROM wp_term_relationships AS txrm
LEFT JOIN wp_term_taxonomy txm ON txrm.term_taxonomy_id = txm.term_taxonomy_id
LEFT JOIN wp_terms trm ON txm.term_id = trm.term_id
WHERE txm.taxonomy = \'mediums\'
AND ( trm.name LIKE \'%Acrylic%\' )
GROUP BY txrm.object_id
HAVING count(trm.name) = 1
) AS trm ON p.ID = trm.object_id
INNER JOIN wp_term_relationships txrk ON p.ID = txrk.object_id
INNER JOIN wp_term_taxonomy txk ON txrk.term_taxonomy_id = txk.term_taxonomy_id
INNER JOIN (
SELECT txrk.object_id FROM wp_term_relationships AS txrk
LEFT JOIN wp_term_taxonomy txk ON txrk.term_taxonomy_id = txk.term_taxonomy_id
LEFT JOIN wp_terms trk ON txk.term_id = trk.term_id
WHERE txk.taxonomy = \'keywords\'
AND ( trk.name LIKE \'%Landscape%\' )
GROUP BY txrk.object_id
HAVING count(trk.name) = 1
) AS trk ON p.ID = trk.object_id
WHERE p.post_status = \'publish\'
AND p.post_type = \'gallery\'
GROUP BY p.ID
ORDER BY p.post_date DESC
因此,基本上,我必须为每个分类法添加一个内部查询,在该查询中,它会发回一个符合条件的术语关系列表,但通过对它们进行分组,我不会得到重复项。