Wordpress Category Post List

24th June 2010 - 3 minutes read time

A friend of mine asked me to write a Wordpress function the other day that printed out a list of categories and any posts in those categories, along with any meta data that might be in the post.

All the function does is to get a list of categories and then for each category get a list of the posts associated with that category. Not much really, but useful in some circumstances.

  1. function getCategoryPostList($args = array())
  2. {
  3. if (is_string($args)) {
  4. parse_str($args, $args);
  5. }
  7. // Set up defaults.
  8. $defaults = array(
  9. 'echo' => true,
  10. );
  12. // Merge the defaults with the parameters.
  13. $options = array_merge($defaults, (array)$args);
  15. $output = '';
  17. // Get top level categories
  18. $categories = get_categories(array('hierarchical' => 0));
  19. // Loop through the categories found.
  20. foreach ($categories as $cat) {
  21. // Print out category name
  22. $output .= '<p><strong>' . $cat->name . '</strong></p>';
  24. // Get posts associated with the category
  25. $tmpPosts = get_posts('category=' . $cat->cat_ID);
  26. // Make sure some posts were found.
  27. if (count($tmpPosts) > 0) {
  28. $output .= '<div>';
  29. // Loop through each post found.
  30. foreach ($tmpPosts as $post) {
  31. // Get post meta data.
  32. setup_postdata($post);
  33. // Print out post information
  34. $output .= '<p><a href="' . get_page_link($post->ID) . '" title="' . $post->post_title . '">' . $post->post_title . '</a></p>';
  35. $output .= '<p>' . $post->post_excerpt . '</p>';
  36. }
  37. $output .= '</div>';
  38. }
  39. }
  41. if ($options['echo'] == true) {
  42. // Print out the $output variable.
  43. echo $output;
  44. }
  46. // Return
  47. return $output;
  48. }

To use the function pop it into your functions.php file and call it in your theme like this:


This will probably work best if you create a custom page template and put the function call in there.

The only problem with this function is that if you have a post in multiple categories then you will find the post appearing multiple times. This wasn't an issue for the person I wrote it for, but if you want to change this and think of a good way to do it then post a comment!


I'm looking to print a list of all my categories and sub categories, but I don't need to see the posts. Can you help me with that code? So where should I place the 48 lines of code? And you say....... " To use the function pop it into your functions.php file and call it in your theme like this: getCategoryPostList(); Is the "functions.php" the one inside the theme that I am using? Where does the " getCategoryPostList(); " code get placed? Thanks for your help. Brett

Brett Szemple (Fri, 01/07/2011 - 06:37)

You are right that the functions.php file is the one inside the current theme you are using, if it doesn't exist then go ahead and create it, WordPress will find it automatically. You put the call to getCategoryPostList() wherever you want the list to appear in your template, which would probably be in one of the sidebar template files. Hope that helps! Phil

This function is awesome, thanks so much!

Note to others: this function will not automatically include posts from a custom post type, but you can add that like so:

$tmpPosts = get_posts('post_type=whatever&category=' . $cat->cat_ID);

Perhaps there's a more elegant option, but that worked for me! Hope it helps somebody.

Melanie Richards (Sun, 05/22/2011 - 06:29)


Your script works nice in normal categories :) but its not work with custom post type taxonomies i change this line

$tmpPosts = get_posts('post_type=headline&category=' . $cat->cat_ID);


But no categories shows :(

abdullah (Wed, 07/13/2011 - 16:46)

I want to add all category in one post and not to show the categories on other post. What is the way of this ? Please helps me out I want to apply on my websites.

hire php devel… (Fri, 04/12/2013 - 09:37)

Add new comment

The content of this field is kept private and will not be shown publicly.