Title: Category Posts Plugin
Last modified: August 18, 2016

---

# Category Posts Plugin

 *  Resolved [mtbello](https://wordpress.org/support/users/mtbello/)
 * (@mtbello)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/category-posts-plugin/)
 * I’m using just upgraded to WordPress 2.2. I’m attempting to use WordPress Category
   Posts v1.0 Plugin ([http://blog.watershedstudio.com/2005/05/wordpress-category-posts-plugin-v10/](http://blog.watershedstudio.com/2005/05/wordpress-category-posts-plugin-v10/)).
   The plugin works great, but I cannot limit the number of posts without getting
   errors.
 * The php reads:
 * <?php
    /* Plugin Name: WP Category Posts Plugin URI: [http://watershedstudio.com/portfolio/software/wp-category-posts.html](http://watershedstudio.com/portfolio/software/wp-category-posts.html)
   Description: List the posts in a specific category Author: Brian Groce Version:
   1.0 Author URI: [http://briangroce.com/](http://briangroce.com/) */
 * function wp_cat_posts( $catID ) {
 *  global $wpdb;
 *  $get_posts_in_cat = “SELECT $wpdb->posts.ID, $wpdb->posts.post_title, “;
    $get_posts_in_cat.
   = “$wpdb->post2cat.post_id, $wpdb->post2cat.category_id “; $get_posts_in_cat .
   = “FROM $wpdb->posts, $wpdb->post2cat “; $get_posts_in_cat .= “WHERE $wpdb->posts.
   ID = $wpdb->post2cat.post_ID “; $get_posts_in_cat .= “AND $wpdb->post2cat.category_id
   = ‘$catID’ “; $get_posts_in_cat .= “AND $wpdb->posts.post_status = ‘publish’ “;
 *  $get_posts_in_cat .= “ORDER BY $wpdb->posts.post_date DESC”;
    $get_posts_in_cat_result
   = mysql_query($get_posts_in_cat);
 *  while ($posts_in_cat_row = mysql_fetch_assoc($get_posts_in_cat_result)) {
    $
   post_title = $posts_in_cat_row[‘post_title’]; $postID = $posts_in_cat_row[‘ID’];
 *  echo ‘[‘ . $post_title . ‘](https://wordpress.org/support/topic/category-posts-plugin/&apos; . get_permalink($postID) . &apos;?output_format=md)‘;
   }}
 * ?>
 * If I add $get_posts_in_cat .= “LIMIT 10”; to limit the number of posts displayed,
   I get error messages. Can anyone see the error in my ways?

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566395)
 * Maybe you’d have better luck with the [Custom Query String plugin](http://drunkenmonkey.org/user/archive/custom-query-string-2.6.zip).
 *  Thread Starter [mtbello](https://wordpress.org/support/users/mtbello/)
 * (@mtbello)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566405)
 * Thanks for the suggestion MichaelH, but that really doesn’t work for me.
 * I’m trying to create two separate boxes on my sidebar, each one containing recent
   posts titles from different categories. Basically, one box is men’s scores and
   the other box is women’s scores from a sports league.
 * In the sidebar I use the line <?php wp_cat_posts(1); ?> and <?php wp_cat_posts(
   2); ?>. This works great for my purposes. However, the list will grow forever.
   I need a way to limit the number of posts.
 * My error may be in the location of the $get_posts_in_cat .= “LIMIT 10”;
 * Any thoughts? I really appreciate the help.
 *  Thread Starter [mtbello](https://wordpress.org/support/users/mtbello/)
 * (@mtbello)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566408)
 * Well, trying again this morning it worked. Thank you for your help.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 10 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566410)
 * Is it something as simple as a space problem?
 * `$get_posts_in_cat .= "LIMIT 10";`
 * should be
 * `$get_posts_in_cat .= " LIMIT 10";`
 *  [jcaudill](https://wordpress.org/support/users/jcaudill/)
 * (@jcaudill)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566909)
 * I’ve updated the code for WP 2.0+ and to support limiting, and CSS class.
 * Put the following in a file, name it wp_category_posts.php and put it in your
   plugins folder. Activate and all should be right in the world.
 *     ```
       <?php
       /*
       Plugin Name: WP Category Posts
       Plugin URI: http://www.itsensellc.com
       Description: List the posts in a specific category - for version 2.0+ only - support optional arguments of limit and css class
       Author: JP Caudill
       Version: 1.0
       Author URI: http://www.itsensellc.com
       */ 
   
       function wp_cat_posts( $catID, $limit=null, $class=null ) {
   
       	global $wpdb;
   
       		$get_posts_in_cat = "SELECT ID,post_title ";
       		$get_posts_in_cat .= "FROM $wpdb->posts ";
       		$get_posts_in_cat .= "WHERE ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$catID') ";
       		$get_posts_in_cat .= "AND post_type = 'post' ";
       		$get_posts_in_cat .= "ORDER BY post_date DESC ";
   
       		if($limit)
       			$get_posts_in_cat .= "LIMIT 0, $limit ";
   
       		$get_posts_in_cat_result = mysql_query($get_posts_in_cat);
   
       	while ($posts_in_cat_row = mysql_fetch_assoc($get_posts_in_cat_result)) {
       	  $post_title = $posts_in_cat_row['post_title'];
       		$postID = $posts_in_cat_row['ID'];	
   
       		echo '<a href="' . get_permalink($postID) . '" class="' . $class .  '">' . $post_title . '</a><br />';
       		}
       }
   
       ?>
       ```
   
 * Use it like:
 * <?php wp_cat_posts(3); ?> – straight links, no styling and no limit
    <?php wp_cat_posts(
   3,10); ?> – straight links, no styling limited to 10 <?php wp_cat_posts(3,10,’
   leftmenu’); ?> – links with a css class of leftmenu appled and limited to 10

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

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

 * 5 replies
 * 3 participants
 * Last reply from: [jcaudill](https://wordpress.org/support/users/jcaudill/)
 * Last activity: [17 years, 11 months ago](https://wordpress.org/support/topic/category-posts-plugin/#post-566909)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
