WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to edit this widget to display related posts from the current category. This code below display only posts from a category that I must select manually. What I want to happen is that the widget shows related posts from that category. Thanks for any help

add_action( 'widgets_init', 'catposts_load_widgets' );

function catposts_load_widgets() {
    register_widget( 'Catposts_Widget' );

class Catposts_Widget extends WP_Widget {

    function Catposts_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'catposts', 'description' => __('Adds posts from a specific category .', "solostream") );
        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'catposts-widget' );
        /* Create the widget. */
        $this->WP_Widget( 'catposts-widget', __('Category Posts Widget', "solostream"), $widget_ops, $control_ops );

    function widget( $args, $instance ) {
        global $post;
        $post_old = $post; // Save the post object.

        extract( $args );

        // If no title, use the name of the category.
        if( !$instance["title"] ) {
            $category_info = get_category($instance["cat"]);
            $instance["title"] = $category_info->name;

        // Get array of post info.
        $cat_posts = new WP_Query("showposts=" . $instance["num"] . "&cat=" . $instance["cat"]);

        /* Before widget (defined by themes). */
        echo $before_widget;

        // Widget title
        echo $before_title;
        if( $instance["title_link"] )
            echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
            echo $instance["title"];
        echo $after_title;

        // Post list
        echo "<div class='cat-posts-widget'>\n";

        while ( $cat_posts->have_posts() )
                <div class="post" id="catpost-<?php the_ID(); ?>">

                    <div class="entry clearfix">

                        <a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php include (TEMPLATEPATH . "/post-thumb.php"); ?></a>

                        <p class="post-title"><a href="<?php the_permalink() ?>" rel="<?php _e("bookmark", "solostream"); ?>" title="<?php _e("Permanent Link to", "solostream"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></p>

                        <?php the_excerpt(); ?>


                    <?php include (TEMPLATEPATH . "/postinfo.php"); ?>

                    <div style="clear:both;"></div>


        echo "</div>\n";

        echo $after_widget;

        remove_filter('excerpt_length', $new_excerpt_length);

        $post = $post_old; // Restore the post object.

    function form($instance) {
            <label for="<?php echo $this->get_field_id("title"); ?>">
                <?php _e( 'Title' ); ?>:
                <input class="widefat" id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />

                <?php _e( 'Category' ); ?>:
                <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>

            <label for="<?php echo $this->get_field_id("num"); ?>">
                <?php _e('Number of Posts to Show'); ?>:
                <input style="text-align: center;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size='3' />

            <label for="<?php echo $this->get_field_id("title_link"); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("title_link"); ?>" name="<?php echo $this->get_field_name("title_link"); ?>"<?php checked( (bool) $instance["title_link"], true ); ?> />
                <?php _e( 'Make widget title link' ); ?>

share|improve this question
what do you mean by related post? – Mubbashar Nov 19 '12 at 11:45

So you want to display posts that are in the 'current category'. This is doable, but you need to think about what you're going to do when there isn't a 'current category' - for instance on index.php. You'll need to decide what to do on a single page as well - do you take a category from the currently viewed post.

The following queries posts:

  • In the same category, when viewing a category,
  • In some category belonging to the post when viewing a single post
  • Otherwise, does not restrict by category

Note showposts is deprecated, use posts_per_page instead:

$args = array(

if( is_category() ){
    $args['cat'] = get_queried_object_id();

}elseif( is_singular() || is_single() ){
    $cats = get_the_terms(get_the_ID(),'category');
    if( $cats ){
       $cat = array_pop($cat);
       $args['cat'] = (int) $cat->term_id;

 $cat_posts = new WP_Query($args);
share|improve this answer
Thanks you :) but this show latest posts instead of related posts. :) Can you please help me to show latest posts only from related category posts ? .. for example I have post "best apples" in category apples ..and I want to show latest posts only from category apples (your code show latest posts from all categories) Thanks – Product Investigation Nov 19 '12 at 15:46
I'll double check my code later - but it shouldn't do. When viewing the 'best apples' category page it should only show posts in 'best apples'. If viewing a post from 'best apples', it will pick a category that post belongs to and just show posts in that category (not necessary 'best apples'). – Stephen Harris Nov 19 '12 at 15:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.