• trixienolix

    (@trixienolix)


    I’ve been trying to work out how to do the following on my homepage:

    I need 2 loops:
    loop 1: shows the first sticky post
    loop2: shows the next 2 sticky posts but does not show the post above.

    I’ve been hacking at some code but cna’t get the no duplicate part to work:

    LOOP1

    <?php $do_not_duplicate = array();
      $my_query = new WP_Query(array('posts_per_page' => 1, 'post__in' => get_option( 'sticky_posts' ) ) );
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>
    do stuff
      <?php endwhile; ?>
      <?php wp_reset_query(); ?>

    LOOP 2

    <?php $my_query = new WP_Query(array('posts_per_page' => 2, 'post__not_in' => $do_not_duplicate, 'post__in' => get_option( 'sticky_posts' ) ) );
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[] = $post->ID; ?>
    do stuff
      <?php endwhile; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • theApe

    (@theape)

    Thread Starter trixienolix

    (@trixienolix)

    hi thanks for answering!
    I tried using offset in my second loop but it still listed the first post, although it put this first post second in the list. I have a feeling offset works a bit strangely with sticky posts because their stickiness means they will do anything to get to the top?!

    Michael

    (@alchymyth)

    one possibility:
    http://pastebin.com/7EarVXds

    other possibility:
    http://pastebin.com/FRMYP8DT

    both use 'ignore_sticky_posts' => true to exclude the uncontrolled output of stickies.

    the first basically subtracts the duplicate post IDs from the sticky post IDs – using array_diff();

    the second uses only one query with 3 posts, but breaks the first part-loop before the endwhile.

    Thread Starter trixienolix

    (@trixienolix)

    alchymyth: thank you so much. I might not have time to check these out today but many thanks for your help. I’ve been following other posts you’ve written and they’ve always been really helpful.

    Hey alchymyth,

    Sorry to piggyback on this post, but it’s gotten me closer than any other toward my particular problem, which is just a little different. I have 2, or potentially 3 loops: in the first I want to show 1 sticky (though others may exist), followed by the next 7 most recent posts, then the next 10 most recent posts. I had something working well, using the following:

    $myposts = new WP_Query('posts_per_page=8');
      while ($myposts->have_posts()) : $myposts->the_post();
      $do_not_duplicate[] = $post->ID?>
    <?php if ( is_sticky() ) :  ?>
    //sticky stuff
    <?php else : ?>
    //first list of 7 posts
    <?php endif; ?>
    <?php endwhile; ?>
    <?php query_posts(array('post__not_in'=>$do_not_duplicate));
     if (have_posts()) : while (have_posts()) : the_post();
     ?>
    //second list of posts

    But the is_sticky doesn’t let me single out just one sticky – it publishes all of them.

    So I’ve tried adapting the code you provided for @trixienolix, but my PHP isn’t good enough to work it out…:(

    Help?
    thnx!
    -jennyb

    @jennybeaumont: It is impolite and contrary to forum policy to interrupt another poster’s ongoing thread with a question of your own. Please post your own topic.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘2 loops, both sticky posts, no duplicate posts’ is closed to new replies.