Wordpress SQL语句排除某些类别

时间:2010-11-29 09:49:12

标签: wordpress mysql wordpress-plugin

我想知道如何重写这个陈述以排除某个类别,即。 category id = 14

请注意,wordpress类别位于不同的表格中。

引用为term_relationships,类别为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); ?>

1 个答案:

答案 0 :(得分:3)

$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')");