How do I limit taxonomy by content type? I want to limit the results of my code snippet below to query only items that have been used in a particular content type. How do I do that

&lt;?php

$vid = 15; //vocabulary id
$num_term = 20; //limit maximum terms

//USE INNER JOIN below to only show terms with a count of 1 or more (the LEFT JOIN includes terms with a count of 0)
$query = "SELECT td.tid AS tid, name, COUNT( nid ) AS count
FROM {term_data} AS td
LEFT JOIN {term_node} AS tn ON td.tid = tn.tid
WHERE td.vid=". $vid ."
GROUP BY tn.tid ORDER BY name ASC LIMIT ". $num_term;
$result = db_query($query);
while ($term = db_fetch_object($result)) {
$items[]= l($term-&gt;name, "taxonomy/term/$term-&gt;tid") . ' (' . $term-&gt;count . ')';
}

if(count($items)) {
return theme('item_list',$items);
}

?&gt;
You would also join in the node table (on term_node.nid = node.nid) and add node.type to the WHERE clause.

Do note there are several problems with your above code. First and foremost, as mariomc asks, any special reason why you're doing this manually instead of through Views?

Second, if you do have a good reason to do the querying yourself, do look into using placeholders with db_query. Its primary purpose is preventing SQL injection problems (strictly speaking, if the variables really are hard coded like this, there is really no danger, but consider what happens when you decide to feed them in from somewhere else at a later date). 

All in all, code that would both answer your question and is more "correct" would be (untested):

&lt;?php

$vid = 15; //vocabulary id
$num_term = 20; //limit maximum terms
$type = 'story'; // Your type

$query = "SELECT td.tid AS tid, name, COUNT( nid ) AS count
  FROM {term_data} AS td
  LEFT JOIN {term_node} AS tn ON td.tid = tn.tid
  LEFT JOIN {node} AS n ON tn.nid = n.nid
  WHERE td.vid=%d AND n.type = '%s'
  GROUP BY tn.tid ORDER BY name ASC LIMIT %d";

$result = db_query($query, $vid, $type, $num_term);

// ...
?&gt;