$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Duplicated posts on category page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - Duplicated posts on category page

matteradmin9PV0评论

So, I have archive.php file with this part of code

<div class="column is-full is-three-quarters p-small">
    <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'loops/loop', 'archive' );
        endwhile;
        else :
            get_template_part( 'template-parts/content', 'none' );
        endif;
    ?>
</div>

in file loop-archive.php I have something like this

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'cat' => '3'
);
$query = new WP_Query($args);

if ($query->have_posts()) :
while ($query->have_posts()) :
    $query->the_post();

    // set default featured image
    $thumb = get_field('post_thumb');
    $post_thumb = ($thumb ? $thumb['url'] : get_template_directory_uri() . '/dist/images/default-post-featured.jpg');

    ?>
    <div class="columns">
        <div class="column is-one-third">
            <div class="article-thumb">
                <?php echo $post_thumb ?>
            </div>
        </div>
        <div class="column is-two-third">
            <article class="content has-text-left">
                <div class="article-head">
                    <header>
                        <h3><?php echo the_title(); ?></h3>
                        <span>dodano: <?php echo get_the_date('d.m.Y') ?></span>
                    </header>
                </div>
                <div class="article-body">
                    <?php echo the_excerpt(); ?>
                </div>
                <div class="article-foot">
                    <a href="<?php echo the_permalink(); ?>" class="button button__rounded button__gold">Więcej</a>
                </div>
            </article>
        </div>
    </div>
<?php
endwhile;
wp_reset_postdata();
else:
?>

<div class="col">
    <h5>Brak wyników spełniających kryteria wyszukiwania</h5>
</div>

and it works. But it duplicate posts. I have 2 post but loop shows 4 posts. I tried using $do_not_duplicate trick but it don't work for me. What's wrong with my code? How to avoid posts duplicate?

So, I have archive.php file with this part of code

<div class="column is-full is-three-quarters p-small">
    <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'loops/loop', 'archive' );
        endwhile;
        else :
            get_template_part( 'template-parts/content', 'none' );
        endif;
    ?>
</div>

in file loop-archive.php I have something like this

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'cat' => '3'
);
$query = new WP_Query($args);

if ($query->have_posts()) :
while ($query->have_posts()) :
    $query->the_post();

    // set default featured image
    $thumb = get_field('post_thumb');
    $post_thumb = ($thumb ? $thumb['url'] : get_template_directory_uri() . '/dist/images/default-post-featured.jpg');

    ?>
    <div class="columns">
        <div class="column is-one-third">
            <div class="article-thumb">
                <?php echo $post_thumb ?>
            </div>
        </div>
        <div class="column is-two-third">
            <article class="content has-text-left">
                <div class="article-head">
                    <header>
                        <h3><?php echo the_title(); ?></h3>
                        <span>dodano: <?php echo get_the_date('d.m.Y') ?></span>
                    </header>
                </div>
                <div class="article-body">
                    <?php echo the_excerpt(); ?>
                </div>
                <div class="article-foot">
                    <a href="<?php echo the_permalink(); ?>" class="button button__rounded button__gold">Więcej</a>
                </div>
            </article>
        </div>
    </div>
<?php
endwhile;
wp_reset_postdata();
else:
?>

<div class="col">
    <h5>Brak wyników spełniających kryteria wyszukiwania</h5>
</div>

and it works. But it duplicate posts. I have 2 post but loop shows 4 posts. I tried using $do_not_duplicate trick but it don't work for me. What's wrong with my code? How to avoid posts duplicate?

Share Improve this question edited Feb 6, 2019 at 16:37 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 6, 2019 at 16:30 MMPL1MMPL1 2671 gold badge2 silver badges8 bronze badges 1
  • BTW. You're missing some if in archive.php, I guess... – Krzysiek Dróżdż Commented Feb 6, 2019 at 16:37
Add a comment  | 

1 Answer 1

Reset to default 0

OK, so the code does exactly what it is supposed to.

In your archive.php you do this:

while ( have_posts() ) : the_post();
    get_template_part( 'loops/loop', 'archive' );

So for every post (both of them) you include loops/loop-archive.php file. And inside that file you don't display only the current post from global loop, but you make your own, custom loop. And this one again iterates through all posts (because you set pre_get_posts to -1.

So your code, after some simplifications looks like this:

while ( have_posts() ) : the_post();
    // the part from get_template_part( 'loops/loop', 'archive' );
    $query = new WP_Query($args);

    if ($query->have_posts()) :
        while ($query->have_posts()) :
            $query->the_post();
            // display the post

So yes - it shows 4 posts instead of 2. And it will display 9 posts instead of 3, if there will be 3 posts - because for every post you display all posts.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far