Ignoring posts in wordpress already used in a loop

Currently using ACF Repeater for WP to show some posts within a category but if I add the same repeater I want it to keep a log of what post ids have been used so it can exclude them from the new loop.

The only problem is my current code works fine for the first loop and the second but adding anymore than two just resets back to the first set of posts. Dumping the array looks like it is not adding to the array just overwriting it.

Read More

First array looks like this

 array(3) { [0]=> int(28890) [1]=> int(28790) [2]=> int(28785) }

Second array

 array(3) { [0]=> int(28749) [1]=> int(1) [2]=> int(28484) }

Third

 array(3) { [0]=> int(28890) [1]=> int(28790) [2]=> int(28785) }

Here is my code

<?php
$cat = get_sub_field('category_name');
$args = array(
    'posts_per_page' => 3,
    'category_name' => $cat,
    'post__not_in' => $ids
);
query_posts( $args );
$ids = array();
?>
<div class="hub-cont">
<?php while (have_posts()) : the_post(); ?>
<?php array_push($ids,get_the_ID()); /*$ids[] = get_the_ID();*/?>
    <div class="blockitem2 small-12 medium-4 large-4">
        <?php
        // Fetch all posts relating to a certain tag then display 4 of them
        //Get the Thumbnail URL
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 720,405 ), false, '' );
        ?>

        <div id="promolink"></div><div class="blockimage" style="background-image: url('<?php echo $src[0]; ?>'); background-repeat: no-repeat; background-size: cover;">

            <div class="cats"><?php echo the_category(' '); ?></div>
        </div>
        <div class="meta">
            <a class="gdbnewslink dark" href="<?php echo get_permalink();?>" ><?php the_title();?> </a>
        </div>
        <div class="clear"></div>
        <div id="newsintro"><?php $text = $post->post_content; $trimmed = wp_trim_words( $text, 50, null ); echo $trimmed; ?></div>
    </div>

<?php endwhile; ?>
    <?php wp_reset_query(); ?>
    <?php var_dump($ids); ?>
</div>

Arrays are still pretty new to me so your guidance will be greatly appreciated!

Related posts

Leave a Reply

1 comment

  1. Here is the solution using information from this link. https://www.binarymoon.co.uk/2010/03/5-wordpress-queryposts-tips/

    Add this to your functions file.

    $bmIgnorePosts = array();
    
    /**
     * add a post id to the ignore list for future query_posts
     */
    function bm_ignorePost ($id) {
        if (!is_page()) {
            global $bmIgnorePosts;
            $bmIgnorePosts[] = $id;
        }
    }
    
    /**
     * reset the ignore list
     */
    function bm_ignorePostReset () {
        global $bmIgnorePosts;
        $bmIgnorePosts = array();
    }
    
    /**
     * remove the posts from query_posts
     */
    function bm_postStrip ($where) {
        global $bmIgnorePosts, $wpdb;
        if (count($bmIgnorePosts) > 0) {
            $where .= ' AND ' . $wpdb->posts . '.ID NOT IN(' . implode (',', $bmIgnorePosts) . ') ';
        }
        return $where;
    }
    
    add_filter ('posts_where', 'bm_postStrip');
    

    Then to use this you would do your loop as normal, and call ‘bm_ignorePost($post->ID);’ for each post you want to ignore. The following example uses the same query twice, but will display totally different posts on each output.

    <?php
    // set the query
    $query = 'posts_per_page=10';
    
    // loop 1 - display most recent 10 posts
    $queryObject = new WP_Query($query);
    if ($queryObject->have_posts()) {
        while ($queryObject->have_posts()) {
            bm_ignorePost($queryPost->post->ID);
            $queryObject->the_post();
            the_title();
            the_content();
        }
    }
    
    // loop 2 - same query, get the next 10 posts
    $queryObject = new WP_Query($query);
    if ($queryObject->have_posts()) {
        while ($queryObject->have_posts()) {
            bm_ignorePost($queryPost->post->ID);
            $queryObject->the_post();
            the_title();
            the_content();
        }
    }
    ?>