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

Could someone tell me how i could make this work? i have a loop, and i would like to create another loop to find the other posts from the same category : but this does not work :

<?php while ( have_posts() ) : the_post(); ?>
            <?php /* How to display all other posts. */ ?>


                $monid = the_ID();
                $cat = get_the_category($monid);  //this works
                $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat );
                $myposts = get_posts( $args );
                var_dump($myposts); //this don't ?>


i found this topic Loop inside the loop but it does not really work for me : any help?

thanks a lot

share|improve this question

the_ID() prints the current post's ID. You need to use get_the_ID() to return it.

Also, get_the_category returns an array of category objects - one foreach category term the post belongs to.

The 'category' attribute expects a category term ID. So you need to pick a category object, then obtain its ID:

  $cats = get_the_category();
  $cat_obj = array_shift($cats);
  $cat_id = (int) $cat_obj->cat_ID;


$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => $cat_id );
share|improve this answer
Thanks a lot Stephen, so cat_id works, but $myposts = get_posts( $args ); does not work: it returns an empty array ... Do know why? – Paul May 18 '12 at 17:27
up vote 0 down vote accepted

Okay, it works like that :

<?php while ( have_posts() ) : the_post(); ?>

                    $cats = get_the_category();
                    $cat_obj = array_shift($cats);

                    $cat_id = (int) $cat_obj->cat_ID;

                    $qq = query_posts( 'cat=$cat_id&posts_per_page=1' );

                    foreach($qq as $q2){


i hope this can help

share|improve this answer

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.