Title: Recent Posts From Specific Category
Last modified: August 18, 2016

---

# Recent Posts From Specific Category

 *  Resolved [Jeffro](https://wordpress.org/support/users/jeffr0/)
 * (@jeffr0)
 * [18 years, 9 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/)
 * I’ve searched the forums and Google high and low and it seems as though I can’t
   find a solution to this problem. I am currently using a block of code that shows
   the most recent 5 posts to my blog. Can anyone show me what they would change
   in this block of code which would allow me to show the 5 recent articles from
   a specific category?
 *     ```
       <? $today = current_time('mysql', 1);
       if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 0, 5")):
       		?><h2>Recent Articles:</h2>
       		<ul id="category_articles">
       			<?php foreach ($recentposts as $post) { if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
       			echo "<li><a href='".get_permalink($post->ID)."'>"; the_title(); echo"</a><small>"; the_date(); echo "</small></li>"; } ?>
       		</ul>
       				<?php endif; ?>
       ```
   

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 9 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-617859)
 * I believe this plugin might help.
    [http://www.coffee2code.com/archives/2004/08/27/plugin-customizable-post-listings/](http://www.coffee2code.com/archives/2004/08/27/plugin-customizable-post-listings/)
 *  [lloydarmbrust](https://wordpress.org/support/users/lloydarmbrust/)
 * (@lloydarmbrust)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618159)
 * You actually don’t need a plugin for this as the WordPress Codex includes this
   feature.
 * Just use the following code:
 *     ```
       <ul>
        <?php
        global $post;
        $myposts = get_posts('numberposts=5&offset=1&category=1');
        foreach($myposts as $post) :
        setup_postdata($post);
        ?>
   
       <li><a>"><?php the_title(); ?></a> --- <?php the_excerpt(); ?></li>
        <?php endforeach; ?>
        </ul>
       ```
   
 * If you don’t want it to post a digest, than use this code:
 *     ```
       <ul>
        <?php
        global $post;
        $myposts = get_posts('numberposts=5&offset=1&category=1');
        foreach($myposts as $post) :
        setup_postdata($post);
        ?>
   
       <li><a>"><?php the_title(); ?></a>
        </li>
       </ul>
       ```
   
 * I didn’t make this up, the smart dudes at WordPress and nerds from around the
   world did. I always suggest searching the WordPress documentation before relying
   on a plug in–those things are so annoying to update!
 *  [lloydarmbrust](https://wordpress.org/support/users/lloydarmbrust/)
 * (@lloydarmbrust)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618160)
 * I should also mention that you need to change the category=1 to = the category
   ID number of your desire.
 * I’d really like to know how to make the category ID generate automatically. As
   in, if you added this code to a specific page within a category, it would pull
   that category ID and display more posts related to that category. I’ve been trying
   to figure this out, but apparently my PHP is lacking.
 *  [Roar](https://wordpress.org/support/users/rori/)
 * (@rori)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618167)
 * The second bit of code lloyd provides returns an error?
 * `parse error: syntax error, unexpected $end in /home/ddddd/public_html/dddd/blog/
   wp-content/plugins/exec-php/runtime.php(41) : eval()'d code on line 11`
 *  [krez](https://wordpress.org/support/users/krez/)
 * (@krez)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618176)
 * i’m getting the exact same error…
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 3 months ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618177)
 * > _plugins/exec-php_
 * The given bits of code above will not work using the ExecPHP plugin. They’re 
   meant to be used in a template.
 * Don’t use PHP Execution plugins. Avoid them if possible. They are bad hacky ways
   to solve problems, at best.
 *  [bnylus](https://wordpress.org/support/users/bnylus/)
 * (@bnylus)
 * [18 years ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618200)
 * The error for the 2nd code example is because the foreach is not closed.
 * Add this to the end and it should work.
    `<?php endforeach; ?>`
 * So the whole thing should look like:
 *     ```
       <ul>
       <?php
        global $post;
        $myposts = get_posts('numberposts=5&offset=1&category=3');
        foreach($myposts as $post) :
        setup_postdata($post);
        ?>
   
       <li><?php the_title(); ?> </li>
       <?php endforeach; ?>
       </ul>
       ```
   

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

The topic ‘Recent Posts From Specific Category’ is closed to new replies.

## Tags

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

 * 7 replies
 * 7 participants
 * Last reply from: [bnylus](https://wordpress.org/support/users/bnylus/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/recent-posts-from-specific-category/#post-618200)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
