0
votes

I have 2 loops. In the 1st loop the 3 most recent sticky posts are shown. In the 2nd posts all posts (sticky and not sticky) are shown. Now I want to remove the 3 most recent sticky posts in the second loop.

I tried something like this but it removes all sticky posts from both loops instead of some sticky posts from loop2. // Edit: Code of both of my loops:

<?php
$sticky = get_option('sticky_posts'); // Get all sticky posts.
rsort($sticky); // Sort sticky posts in reverse order.
$sticky = array_slice($sticky, 0, 3); // Number of sticky posts to show.
$latestStickyPosts = $sticky;

$loop1query = new WP_Query(array('post__in' => $sticky, 'ignore_sticky_posts' => 1));

// START 1ST LOOP.
while ($loop1query->have_posts()) : $loop1query->the_post();
$do_not_duplicate[] = $post->ID; // global. ?>
<div> ... </div>
<?php endwhile;
// END 1ST LOOP.


$showposts = 5; // Show posts per page. 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'paged' => $paged);

$loop2query = new WP_Query($args);

// START 2ND LOOP.
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); 
if( in_array($post->ID, $latestStickyPosts ) continue; ?>
<div> ... </div>
<?php endwhile; endif;
// END 2ND LOOP. ?>

I don't care if the posts are really not sticky in the backend. They just should be displayed in that way: loop1 = 3 most recent stickies. loop2 = rest of the stickies and all not sticky posts

Thank you for any help or advise!

1

1 Answers

0
votes

I don't see the first loop in your code, but since you're looping through all the posts in the second one, you could keep an array with the 3 IDs of the sticky posts you don't need to show again.

Suppose you have an array $latestStickyPosts, then you can do:

query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); 
if (in_array($post->ID, $latestStickyPosts)) continue; ?>

For more information, check this documentation.