Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to modify a plugin that generates an archive listing so it shows only one category, making it a single category archive.

The old version of the plugin used a get_posts query, and so it was easy to disallow categories of posts:

$rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );

The new version of the plugin uses that database query:

SELECT ID, post_date, post_date_gmt, comment_status, comment_count FROM 
$wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_password =

How do I disallow several categories from a database query?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You could use the get_tax_sql() function introduced in WP 3.1:

$tax_query = array(
  array(
    'taxonomy' => 'category',
    'terms' => array( 4, 6, 7, 9 ),
    'operator' => 'NOT IN'
  )
);

$clauses = get_tax_sql( $tax_query, $wpdb->posts, 'ID' );

...
"SELECT ID, post_date, post_date_gmt, comment_status, comment_count
FROM $wpdb->posts {$clauses['join']}
WHERE post_status = 'publish'
AND post_type = 'post'
{$clauses['where']}
"
...

(not tested)

share|improve this answer
    
Works great! Thanks. –  songdogtech Mar 23 '11 at 3:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.