I’m modifying a wordpress plugin (Related Posts by Category) to make it instead “Related Posts by Taxonomy”. I’ve created multiple taxonomies (elements, colors, and mood) that I’d like to include in this query, but can only seem to make it work with one taxonomy (elements). The code below only returns related posts that have the same “elements”. What am I doing wrong? I’d like the code to return posts that match either one, both, or all taxonomies, not just elements.
$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) : '')
)
Try changing
AND t.taxonomy = 'elements'
to;