Title: Get Posts: Category
Last modified: August 19, 2016

---

# Get Posts: Category

 *  Resolved [jonizen](https://wordpress.org/support/users/jonizen/)
 * (@jonizen)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/)
 * Hey guys,
    When looking at a post, I need it to bring up a list of only the posts
   in its category. Currently it lists all posts regardless of category.
 * I’ve figured out how to make it static
    $postslist = get_posts(‘category_name
   =news’);
 * I need it to be dynamic.
 * Here is my relevant code
 *     ```
       <ul>
         <?php
           $postslist = get_posts();
           foreach ($postslist as $post) :
           setup_postdata($post);
         ?> 
   
       <li>
          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       </li>
       <?php endforeach; ?>
       </ul>
       ```
   
 * [**moderated–bump removed.** Please refrain from bumping as per [Forum Rules](http://wordpress.org/support/topic/68664?replies=6)]

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993036)
 * Something for in ‘standard’ [loop](http://codex.wordpress.org/The_Loop)?
 *     ```
       <?php $post_cats=wp_get_post_categories($post->ID);?>
       <ul>
       <?php
         $args=array(
           'showposts'=>4,
           'cat'=>$post_cats[0],
           'post__not_in' => array($post->ID)
         );
           $postslist = get_posts($args);
        if ($postslist) {
           foreach ($postslist as $mypost) :
           ?>
             <li>
             <a href="<?php echo get_permalink($mypost->ID); ?>"><?php echo $mypost->post_title; ?></a>
             </li>
           <?php endforeach;
         }
       ?>
       </ul>
       ```
   
 * Note: might also look at similar/related posts type plugins:
    [http://rmarsh.com/plugins/similar-posts/](http://rmarsh.com/plugins/similar-posts/)
   [http://wordpress.org/extend/plugins/tags/related](http://wordpress.org/extend/plugins/tags/related)
 *  Thread Starter [jonizen](https://wordpress.org/support/users/jonizen/)
 * (@jonizen)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993131)
 * I can’t seem to get this to work Michael. The result is a link to the first post.
 * I need a way to find out the current category for the post, and then I need to
   run a loop grabbing the posts in the same category.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993132)
 * > I need a way to find out the current category for the post
 * That’s this:
 *     ```
       <?php $post_cats=wp_get_post_categories($post->ID);?>
       ```
   
 * and `$post_cats[0]` is the ID of the 1st category returned for the post (posts
   can have multiple categories)
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993133)
 * Here’s the index.php for use with the WordPress Default Theme:
 * [http://wordpress.pastebin.ca/1342102](http://wordpress.pastebin.ca/1342102)
 *  Thread Starter [jonizen](https://wordpress.org/support/users/jonizen/)
 * (@jonizen)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993136)
 * It works, thank you

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Get Posts: Category’ is closed to new replies.

## Tags

 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [jonizen](https://wordpress.org/support/users/jonizen/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/get-posts-category/#post-993136)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
