Support » Fixing WordPress » Two loops – one for sticky, one for the rest

  • Resolved cmegown


    Afternoon everyone 🙂

    I’ve got a little issue I can’t seem to resolve on my own, so I turn to you, the amazing WordPress community!

    Here’s the skinny on my code:

        // hack, shows only the most recent sticky post
        $sticky = get_option('sticky_posts');
        $wp_query = new WP_Query(array('post_in' => $sticky, 'caller_get_posts' => 1, 'orderby' => ID, 'showposts' => 1));
    <?php while (have_posts()) : the_post(); ?>
        <!-- loop code, not important here -->
    <?php endwhile; wp_reset_query(); ?>
    <?php query_posts(array('post__not_in' => get_option('sticky_posts'))); // exclude all sticky posts ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <!-- more loop code, not important either -->
        <?php endif; wp_reset_query(); ?>

    The issue here is that it looks beautiful on the front page with my single most recent sticky post on top, followed by every other post (excluding stickies). However, once I page over, the same 5 posts from the 2nd loop that were on the front page continue to show again and again.

    I’d appreciate some help, thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • I think this may point you in the right direction.

    I had tried that before, because technically my first loop is in a “featured.php” file which I include just before the main loop on index.php

    However, this did not work either. Let me know if you want me to post a more detailed snippet of my code.

    Just to clarify what you are looking for… when you page over, you want the same sticky post to show on every page, but you want the main loop to show the next posts?

    No, I want the first sticky to show ONLY on the front page, then each page after that will not display any sticky posts



    Forum Moderator

    maybe use an “if” statement on the sticky post query? “if is home”

    never mind, esmi to the rescue.



    Forum Moderator

    Let’s see if that code I posted works first 🙂

    looks like you’re saying don’t show stickies when the query is paged..? I just dicovered query parameters in the codex, but I’m sure I’m getting ahead of myself. I would have never thought of paging as a query with paramaters.. but I suppose that’s all it could be.

    can’t wait, I’m going to try it me-self.

    oh well, the white pages obviously mean I don’t understand how it’s used, so I can’t test it 🙁

    Thank you for your replies everyone, but after a few hours of research and tinkering (and some great help from Chip Bennett), here is the solution:

    The Problem: I want one loop to show only my most recent sticky post to show on top of the FRONT PAGE ONLY, and a second loop to display all other posts (except sticky posts) with working pagination.

    // 1st loop - index.php
    // Get IDs of sticky posts
    $sticky = get_option('sticky_posts');
    // First loop to display only my single, most recent sticky post
    $most_recent_sticky_post = new WP_Query(array(
        // Only sticky posts
        'post__in' => $sticky,
        // Treat them as sticky posts
        'ignore_sticky_posts' => 1,
        // Order by ID
        'orderby' => ID,
        // Get only the most recent
        'posts_per_page' => 1
    while ($most_recent_sticky_post->have_posts()) : $most_recent_sticky_post->the_post(); ?>
        <!-- loop code -->
    <?php endwhile; wp_reset_query(); ?>
    // 2nd loop - index.php
    <?php $all_other_posts = array(
        'post__not_in' => get_option('sticky_posts');
    global $wp_query;
    $merged_query_args = array_merger($wp_query->query, $all_other_posts);
    query_posts($merged_query_args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <!-- loop code -->
    <?php endif; wp_reset_query(); ?>

    This structure of multiple loops allows the theme to display only the single, most-recent sticky post at the top of the front page. After that, all other posts show except for any sticky posts. This wonderful solution was provided in this post by Chip Bennett.

    Also, pagination works like a charm!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Two loops – one for sticky, one for the rest’ is closed to new replies.