Title: Avoid duplicate posts using &#8216;pre_get_posts&#8217;
Last modified: August 17, 2020

---

# Avoid duplicate posts using ‘pre_get_posts’

 *  [marcorroma](https://wordpress.org/support/users/marcorroma/)
 * (@marcorroma)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/)
 * I changed my categories into pages with post blocks created with a page builder(
   WPbakery). How can I avoid duplicate posts using WordPress ‘pre_get_posts’ hook?
   For example. One category has many post blocks, these post blocks also refer 
   to other categories (no subcategories).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Mangesh Nadekar](https://wordpress.org/support/users/mangeshnadekar15/)
 * (@mangeshnadekar15)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/#post-13268398)
 * Hi [@marcorroma](https://wordpress.org/support/users/marcorroma/),
 * To avoid duplicate posts add following code in your active theme functions.php
 * add_filter(‘post_link’, ‘cr_search_displayed_posts’);
    add_action(‘pre_get_posts’,’
   cr_remove_already_displayed_posts’);
 * $displayed_posts = [];
 * function cr_search_displayed_posts($url) {
    global $displayed_posts; $displayed_posts[]
   = get_the_ID(); return $url; // don’t mess with the url }
 * function cr_remove_already_displayed_posts($query) {
    global $displayed_posts;
   $query->set(‘post__not_in’, $displayed_posts); }
 *  Thread Starter [marcorroma](https://wordpress.org/support/users/marcorroma/)
 * (@marcorroma)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/#post-13268456)
 * I copied your code into my theme’s functions but it doesn’t seem to work.
    I 
   cleared the cache of these two pages (home and category), if you want to check:
   [https://www.corriereromagna.it/](https://www.corriereromagna.it/) [https://www.corriereromagna.it/ravenna-notizie/](https://www.corriereromagna.it/ravenna-notizie/)
 *  Thread Starter [marcorroma](https://wordpress.org/support/users/marcorroma/)
 * (@marcorroma)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/#post-13269627)
 * [@mangeshnadekar15](https://wordpress.org/support/users/mangeshnadekar15/)
    This
   code works (I was wrong to copy and paste the code) Thank you!
 *  [Mangesh Nadekar](https://wordpress.org/support/users/mangeshnadekar15/)
 * (@mangeshnadekar15)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/#post-13272411)
 * Hi [@marcorroma](https://wordpress.org/support/users/marcorroma/),
 * I am glad that it works for you.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Avoid duplicate posts using ‘pre_get_posts’’ is closed to new replies.

## Tags

 * [Avoid](https://wordpress.org/support/topic-tag/avoid/)
 * [duplicate](https://wordpress.org/support/topic-tag/duplicate/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Mangesh Nadekar](https://wordpress.org/support/users/mangeshnadekar15/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/avoid-duplicate-posts-using-pre_get_posts/#post-13272411)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
