ApPeL
ApPeL

Reputation: 4911

Wordpress SQL statement to exclude certain category

I am wondering how I can rework this statement to exclude a certain category, ie. category id = 14

Please note that the wordpress categories are in a different table.

referenced as term_relationships and the category is term_taxonomy_id

<?php 
$now = gmdate("Y-m-d H:i:s", strtotime('0 days'));

$request = $wpdb->prepare("SELECT ID, post_title, post_date, post_excerpt,LEFT(post_content,$sqllimit) AS short_post_content FROM $wpdb->posts WHERE post_status = 'publish' ");
if($hide_pass_post) $request .= "AND post_password ='' ";
if($include_pages) $request .= "AND (post_type='post' OR post_type='
else $request .= "AND post_type='post' ";

$request .= "AND post_date_gmt > '$now' ORDER BY post_date ASC LIMIT $skip_posts, $returnnum";
$posts = $wpdb->get_results($request); ?>

Upvotes: 2

Views: 1503

Answers (1)

ApPeL
ApPeL

Reputation: 4911

$request = $wpdb->prepare("SELECT * 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)  

  WHERE ($wpdb->term_taxonomy.term_id = 3
     AND $wpdb->term_taxonomy.term_id <> 14  
     AND $wpdb->term_taxonomy.taxonomy = 'category'   
     AND $wpdb->posts.post_type = 'post'  
     AND $wpdb->posts.post_status = 'publish')");

Upvotes: 3

Related Questions