1

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?

3

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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