views:

116

answers:

1

Hello,

I'm using the query below to output all links from a custom taxonomy. It outputs all posts that are tagged 'http' from the taxonomy 'words'.

I would like to exclude some general categories from the output. So, fe. it only outputs links in the media and news categories. What would be the best way to achieve this?

$wp_query->request = "

SELECT DISTINCT *

FROM $wpdb->posts

LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)

LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)

LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)

WHERE $wpdb->posts.post_type = 'post' 

AND $wpdb->posts.post_status = 'publish'

AND $wpdb->term_taxonomy.taxonomy = 'words'

AND $wpdb->terms.slug = 'http'

ORDER BY $wpdb->posts.post_date DESC

LIMIT $ppp OFFSET $offset";

$pagelinkposts = $wpdb->get_results($wp_query->request, OBJECT);
A: 

I somehow found the solution myself by using codes from another plugin. If someone else want to achieve the same thing add the below before the ORDER BY line (and change category-ids to the ones you want to include.)

AND object_id IN
           (
            SELECT object_id
            FROM $wpdb->term_relationships AS r
            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
            WHERE   x.taxonomy = 'category'
            AND t.term_id IN  (category-ids))
Nordin