How to List all Category IDs from a WordPress MySQL Databese

Posted on 31 October 2010

In my day to day work, one of my jobs is to setup word-press  themes for my clients. Yesterday, I was setting up and configuring a theme that shows latest post from each category in the middle column. The site was over an year old and there were a lot of categories accumulated over time.

Theme had an option where I can put all the categories that I don’t want to display. The theme developer saved time by no providing an interface to select / remove categories. the lazy developer just put a text box where I had to list unwanted categories in comma separated fashion. wordpress mysql query to list categories

Now the challenge for me here was to ask the client which categories they wish to exclude. But in order to do that, I had to provide them a list of all categories. I could simply go to category section of wordpress admin dashboard but catagory ids are not listed there.  so I had to fire up phpMyAdmin and look for a solution in MySQL database of wordpress.


Relevant Tables

I found out that there are three tables.

  1. WP-TERMS
  2. WP-TERMS-TAXONOMY
  3. WP-TERM-RELATIONSHIPS

WP-TERMS table contains all tags and categories and god knows what else.

In WP-TERM-TAXONOMY table, I found that all term ids got defined as tags or categories etc. so i have wrote following MySQL Query to list all categories with their category id and names.

MySQL Query to get Category IDs from WordPress DB

SELECT wp_terms.term_id, wp_terms.name,wp_term_taxonomy.taxonomy FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_term_taxonomy.taxonomy = ‘category’

I have shared this here in case someone might need to accomplish the same task of listing all category ids in wordpress. I can be a place for my future reference as well. :) cheers folks.

People who liked this Post also read

Calendar

October 2010
M T W T F S S
« Sep   Nov »
  1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31