我正在修改一个wordpress插件(按类别分类相关帖子),而不是“分类相关帖子”。我创建了多个分类法(元素,颜色和情绪),我想在此查询中包含这些分类法,但似乎只能使它与一个分类法(元素)一起使用。以下代码仅返回具有相同“元素”的相关帖子。我究竟做错了什么?我希望代码能够返回与一个,两个或所有分类法匹配的帖子,而不仅仅是元素。
$posts = $GLOBALS['wpdb']->get_results(
sprintf(
"SELECT DISTINCT object_id as ID, post_title
FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t, {$GLOBALS['wpdb']->posts} p
WHERE t.term_id IN (SELECT t.term_id FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t
WHERE r.term_taxonomy_id = t.term_taxonomy_id
AND t.taxonomy = 'elements'
AND r.object_id = $id)
AND r.term_taxonomy_id = t.term_taxonomy_id
AND p.post_status = 'publish'
AND p.ID = r.object_id
AND object_id <> $id %s %s %s",
($type ? ("AND p.post_type = '" .$type. "'") : ''),
($orderby ? ('ORDER BY ' .(strtoupper($params['orderby']) == 'RAND' ? 'RAND()' : $orderby. ' ' .$order)) : ''),
($limit ? ('LIMIT ' .$limit) : '')
)
答案 0 :(得分:1)
尝试将AND t.taxonomy = 'elements'
更改为;
AND t.taxonomy IN ('elements', 'mood', 'colors')