【问题标题】:Query multiple taxonomies查询多个分类
【发布时间】:2010-07-06 20:43:46
【问题描述】:

我正在修改一个 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) : '')
)

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试将AND t.taxonomy = 'elements' 更改为;

    AND t.taxonomy IN ('elements', 'mood', 'colors')
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多