-
venchy_123
Hi there,
Support provided me with this code to remove duplicate posts from the homepage when using query loop blocks. And to add this class: remove-latest-posts to the query loop grid block.
It works mostly. However, one section of the homepage contains duplicate posts. This happens when the block at the top and the said section belong to the same category.
Please see the private box for more details.
Here’s the code:
add_filter( ‘generateblocks_query_loop_args’, function( $query_args, $attributes ) {
$args = array(
‘post_type’ => ‘post’,
‘posts_per_page’ => 1,
);
$latest_posts = new WP_Query($args);
$latest_post_ids = wp_list_pluck($latest_posts->posts, ‘ID’);
if (
! empty( $attributes[‘className’] ) &&
strpos( $attributes[‘className’], ‘remove-latest-posts’ ) !== false
) {
// Merge the current $query_args which contains arguments defined in the editor with your ordering arguments
return array_merge( $query_args, array(
‘post__not_in’ => $latest_post_ids,
) );
}return $query_args;
}, 10, 2 ); -
Fernando
Hi there,
Add an Offset parameter to the right Query Loop Block. Set the Offset to 1.
-
venchy_123
It already has an offset 1.
-
osaopaulo
If I may contribute; long ago someone gave me this code to avoid repeating posts everywhere.
It works just fine for me.add_filter('post_link', 'track_displayed_posts'); add_action('pre_get_posts','remove_already_displayed_posts'); $displayed_posts = []; function track_displayed_posts($url) { global $displayed_posts; $displayed_posts[] = get_the_ID(); return $url; // don't mess with the url } function remove_already_displayed_posts($query) { global $displayed_posts; $query->set('post__not_in', $displayed_posts); }
-
venchy_123
Hi. Thanks for your input.
Do I need to add any offset parameters to the query loops?
Or just adding this code should be enough?!
-
For the query loop in the right columns, add the
remove-latest-post
class to it, and setoffset
to1
.
https://app.screencast.com/FAJK4SXKnj9w4 -
venchy_123
Hi,
Still doesn’t remove the duplicate post. -
What’s the
offset
value set for the left query loop? -
venchy_123
There’s no offset value added.
I only added offset value 1 for the right column.
-
That’s the correct setting.
And it should work.
Can you try disabling the snippet, refreshing the page, then re-enabling the snippet, and checking the queue loop changes?
- You must be logged in to reply to this topic.