How to Filter Duplicate Posts in the WordPress Loop

Home » Snippets » How to Filter Duplicate Posts in the WordPress Loop
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of seeing the same posts appear multiple times in your WordPress loop? Whether you’re displaying a list of blog posts or any other type of content, it can be frustrating to have duplicates cluttering up your page. In this article, we’ll show you how to easily filter out duplicate posts from your loop, ensuring that your content remains fresh and unique. Don’t let duplicate posts get in the way of a streamlined user experience – let’s get started on cleaning up your loop.

					function wpturbo_filter_duplicate_posts( $query ) {
    if ( $query->is_main_query() && $query->is_home() ) {
        $query->set( 'post__not_in', get_option( 'wpturbo_duplicate_posts' ) );
    }
}
add_action( 'pre_get_posts', 'wpturbo_filter_duplicate_posts' );

function wpturbo_store_duplicate_posts( $post_id ) {
    $duplicate_posts = get_option( 'wpturbo_duplicate_posts', array() );
    $duplicate_posts[] = $post_id;
    update_option( 'wpturbo_duplicate_posts', $duplicate_posts );
}
add_action( 'save_post', 'wpturbo_store_duplicate_posts' );

function wpturbo_reset_duplicate_posts() {
    delete_option( 'wpturbo_duplicate_posts' );
}
register_activation_hook( __FILE__, 'wpturbo_reset_duplicate_posts' );
register_deactivation_hook( __FILE__, 'wpturbo_reset_duplicate_posts' );
```
				

The code snippet provided tackles the issue of filtering and excluding duplicate posts from the loop in WordPress.

Let’s break down the code and examine what each function does:

  1. wpturbo_filter_duplicate_posts($query) This function is hooked into the pre_get_posts action, which allows us to modify the query before it is executed. It checks if the current query is the main query and if it is the homepage, using $query->is_main_query() and $query->is_home() respectively. If both conditions are true, it sets the post__not_in parameter of the query to the array of post IDs stored in the 'wpturbo_duplicate_posts' option using get_option().

  2. wpturbo_store_duplicate_posts($post_id) This function is hooked into the save_post action, which executes whenever a post is saved or updated. When called, it retrieves the current array of duplicate post IDs stored in the 'wpturbo_duplicate_posts' option using get_option() and appends the new $post_id to the array. Finally, it updates the 'wpturbo_duplicate_posts' option with the updated array using update_option().

  3. wpturbo_reset_duplicate_posts() This function is responsible for resetting or removing the array of duplicate post IDs from the 'wpturbo_duplicate_posts' option. It is hooked into both the register_activation_hook() and register_deactivation_hook() functions, which ensures that the array is reset when the plugin is activated or deactivated. It achieves this by using delete_option() to remove the 'wpturbo_duplicate_posts' option.

To summarize the process:

  • When a post is saved or updated, the wpturbo_store_duplicate_posts() function is triggered and the post ID is added to the array of duplicate post IDs stored in the 'wpturbo_duplicate_posts' option.
  • When the homepage is loaded and the main query is executed, the wpturbo_filter_duplicate_posts() function is called. It retrieves the array of duplicate post IDs from the 'wpturbo_duplicate_posts' option and excludes those IDs from the query using the post__not_in parameter.
  • When the plugin is activated or deactivated, the wpturbo_reset_duplicate_posts() function removes the array of duplicate post IDs.

This code snippet provides an effective solution for filtering and excluding duplicate posts from the loop, ensuring that only unique posts are displayed in the WordPress loop on the homepage.

Register an account to save your snippets or go Pro to get more features.