Just a note before: the question refers to both my answers and editing / adding onto people's answers.
Lately, I've been trying to be more active on the WordPress StackExchange by contributing, as the community has helped me tremendously. Given my experience is not as great as of yet, I'm stuck to menial tasks at best.
But sometimes I stumble over this tiny thing that kills your theme's blog posts.
The horror of the duplicate posts.
Here's an example of a good answer that lacks the patch:
Displaying the first, second, and third posts from a category in separate slides
Great answer. Except if you had multiple "layout widgets", say, a slider, on your homepage, you'd see duplicate posts.
Is this bad? Very. From an SEO perspective, common-sense and UX.
The fix? I can't vouch for whether this is a great way to do it or not, but it works, adding a global and storing each post you've looped through before to that list, simply to avoid using that same post in your next widgets:
global $duplicated_posts;
..
$query = new \WP_Query( $args );
if ( $query->have_posts() ) : ?>
..
<?php
while ( $query->have_posts() ) : $query->the_post();
$duplicated_posts[] = get_the_ID();
?>
And then finally using it in your WP_Query
:
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'order' => $settings['order'],
'orderby' => $settings['orderby'],
'post__not_in' => $duplicated_posts
);
Fixes it and solves an issue many theme developers overlooked.
I believe this is an incredibly critical piece to any theme that wishes to adhere to today's standards, but many questions omit it.
Adding this clause to the answer is certainly off-topic, yet so important.
What can I do here to help improve the situation, assuming everyone else agrees with me this is an issue?
Here's a rather funny depiction of my view on things:
post__not_in
is super expensive, and to be avoided at all costs, it's much faster to filter out posts in PHP