Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Remove duplicate posts from the homepage (Query Loop)

  • 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 );

  • Hi there,

    Add an Offset parameter to the right Query Loop Block. Set the Offset to 1.

  • It already has an offset 1.

  • 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);
    }
    
  • 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 set offset to 1.
    https://app.screencast.com/FAJK4SXKnj9w4

  • Hi,
    Still doesn’t remove the duplicate post.

  • What’s the offset value set for the left query loop?

  • 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?

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.