• neonWired

    (@neonwired)


    I’m using get_posts() to create a secondary list of posts on a page rather than another loop as suggested in the docs, however it’s changing the existing loop from displaying the correct page contents to the first post that get_posts(). Am i missing something here?

    first list:

    $posts = get_posts();
    foreach( $posts as $post ) :	setup_postdata($post); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <p><?php the_date(); ?></p>
    <h3 class="entry-title"><?php the_title(); ?></h3>
    <div class="entry-content">
    <?php
    global $more;
    $more = 0;
    echo strip_tags(get_the_content());
    ?>
    </div>
    </div>
    <?php endforeach; ?>

    main loop:

    <?php wp_reset_query(); ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php if ( !is_front_page() ) { ?>
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <?php } ?>
    <div class="entry-content">
    <?php the_content(); ?>
    </div>
    </div>
    <?php endwhile; ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)

    Try adding <php wp_reset_query();?> after the end of the secondary loop.

    Thread Starter neonWired

    (@neonwired)

    Thanks for your suggestion, i found the bug in my code, it’s actually the use of the variable $posts in $posts = get_posts().

    I assume that this means $posts is used by the loop but unless i declare it as a global shouldn’t it be local and not effect the loop? and shouldn’t wp_reset_query() reset it anyway?

    esmi

    (@esmi)

    Well spotted! $posts may be declared as global in a number of places in WP core.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘problem with get_posts() effecting the loop’ is closed to new replies.