Add Snippet To 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:
-
wpturbo_filter_duplicate_posts($query)
This function is hooked into thepre_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 thepost__not_in
parameter of the query to the array of post IDs stored in the'wpturbo_duplicate_posts'
option usingget_option()
. -
wpturbo_store_duplicate_posts($post_id)
This function is hooked into thesave_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 usingget_option()
and appends the new$post_id
to the array. Finally, it updates the'wpturbo_duplicate_posts'
option with the updated array usingupdate_option()
. -
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 theregister_activation_hook()
andregister_deactivation_hook()
functions, which ensures that the array is reset when the plugin is activated or deactivated. It achieves this by usingdelete_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 thepost__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.