Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer

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() ){ ?>
    <ul>

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

    </ul>
<?php 
} else {
    // no posts found
}

wp_reset_postdata();
share|improve this answer
    
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. –  whiteletters and blankspaces Mar 3 '13 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. –  helgatheviking Mar 3 '13 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 –  whiteletters and blankspaces Mar 3 '13 at 17:41
    
Yes it is supposed to be wp_reset_postdata.. that was a typo in my previous comment. –  helgatheviking Mar 3 '13 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 '13 at 21:19

Your Answer

 
discard

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.