Inside of my loop in single.php, I used a custom query using get_posts to return posts belonging to a certain category.

<?php                                                   global $post;
                                                        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                                                        $myposts = get_posts("paged=$paged&category=5");?>

<?php foreach($myposts as $post) :?>

<?php the_title();?>

<?php endforeach; ?>

The problem is that the original loop, which is the single.php loop, seem to do not work after the execution of the query. For instance, the navigation links (next and previous) dont work.

Have you ever faced this issue? your help is appreciated.

2 Answers 2


You need to call wp_reset_postdata() after your query to restore the global $post variable that the navigation link functions use to determine the next/previous posts.


You could also use WP_Query instead of get_posts. Does about the same thing though, but for reference's sake:

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args =  array ( 'category' => 5, 'paged' => $paged, 'posts_per_page'=> 5 );
$catPosts = new WP_Query( 'post_type=post' );

if( $catPosts->have_posts() ){ ?>

    <?php while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
            <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

} else {
    // no posts found

  • Thank you helgatheviking I agree with you and I really believe in the importance of reading about WP_Query vs get_posts. But I want to add that changing only this didnt resolve the problem. I was foregtting the wp_reset_query(); and that was the lacking function. have a good day. Mar 3, 2013 at 17:32
  • Yes you were stomping on the global $post variable and not resetting it. With get_posts() you need to reset_post_data() and with new WP_Query() you need to wp_reset_query(). Glad you solved it! Cheers. Mar 3, 2013 at 17:34
  • well explained. I add that reset_post_data() or as I did : wp_reset_postdata() both work with get_posts(). and with new WP_Query(), it is wp_reset_query(). I hope I used the exact words. Thank you Mar 3, 2013 at 17:41
  • Yes it is supposed to be wp_reset_postdata.. that was a typo in my previous comment. Mar 3, 2013 at 19:37
  • I think in both cases you need to just wp_reset_postdata. wp_reset_query restores the global $wp_query then calls wp_reset_postdata. if you don't touch the global $wp_query, then there's no query to reset. get_posts itself just creates a new WP_Query instance, so it stands to reason that if you don't need to reset the query with that, you also don't need to with a custom WP_Query.
    – Milo
    Mar 3, 2013 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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