Wp-query 'post__not_in' doesn't work when I want to exclude some posts

Viewed 27

Here is my code and I'm trying to exclude some posts that have been added to the custom post type but it's not working for me, please if anyone can help me??

<div class="grid-x small-up-1 medium-up-3 large-up-3 gridi">
        <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
        <?php 
            $args = array(
                'post_type' => 'gallery',
                'posts_per_page' => -1,
                'post__not_in' => array (779, 1394, 774, 3278),
            );
            $loop = new WP_Query( $args );
        ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="cell">
                <div class="item-navigation">
                    <a href="<?php the_permalink(); ?>">
                        <div class="image-container">
                        <?php the_post_thumbnail(); ?>
                            <div class="description">
                                <h5 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                                <p><?php the_excerpt(); ?></p>
                                <a class="read-more" href="<?php the_permalink(); ?>">Read More</a>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        <?php endwhile; endif; ?>
    </div>
1 Answers

This line..:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

... simply executes the global wp_query. The query you declare the line above as $loop isn't used.

Try:

<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
Related