How to List All Categories, Pages and Their ID in WordPress

Posted in Wordpress Development on 16 December 2010 2 comments

If you are a developer, in some cases you need to get all categories and their IDs. You might start referring to wordpress codex but most likely you will end up with no solution, just like me. Rather than counting on wordpress’s built-in function, you can write your own MySQL query to accomplish this.

MySQL query that pulls all categories from database

';
}

?>

$table_prefix is a variable declared in wp-config.php, it carries value “wp_” by default unless you changed the setting. It is your SQL tables’ prefix.

WordPress stores all category, tags, etc in wp_term_taxonomy table so we can load all categories name and ID from there.

What about Pages?

';
}

?>

WordPress stores all pages in wp_posts table, but with post_type set as “page”.

Hope this helps.

 

Posted by Zen on 16 December 2010 2 comments
Tags :



or Subscribe to specific category only :




  - 2 Comments


Frank Torres says:

Awesome code! Just an FYI, if you’re using this in a function (like I am), you need to have “global $table_prefix;” (no quotes) prior to the line that starts “$result = mysql_query(“.

Shir Gans says:

Hi, I’ve been looking for a solution in google… but nothing. It was really hard to get this piece of code. Thanks !

Leave a Reply

You must be logged in to post a comment.

Previous Post
«
Next Post
»