WordPress – How to Query the Categories and not the Tags in WordPress

Today I had found I had a problem with an old plugin that queried terms table for categories. It seemed after upgrading WordPress to the latest version, I was getting all of the tags along with the categories. All I wanted was the categories.

Well I found that the results of the terms table also included the tags(which were previously in the posts table. After some research I found that this change was implemented over a year ago. So much for me keeping up with the changes.

Well I had to figure out how WordPress differenciates between tags and categories, This is were the term_taxonomy table comes into play. The ID of the category or term from the terms table is placed in the term_taxonomy table and gets identified with either category or post_tag in the taxonomy fieldof term_taxonomy.

$r = mysql_query("SELECT * FROM {$table_prefix}terms, {$table_prefix}term_taxonomy
WHERE {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
AND {$table_prefix}term_taxonomy.taxonomy = 'category'");

Leave a Reply

Your email address will not be published. Required fields are marked *