Disallow categories from this MySQL query

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:

Read More
$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?

Related posts

Leave a Reply

1 comment

  1. 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)