Title: Widgets &amp; WP Query &amp; duplicate posts
Last modified: August 22, 2016

---

# Widgets & WP Query & duplicate posts

 *  [Marek Železný](https://wordpress.org/support/users/igreenie/)
 * (@igreenie)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/)
 * Hi, sometimes we need to place some ads between posts on homepage, so I decided
   to use widgets and sidebar on homepage. For example, Post Widget / Post Widget/
   Ads / Post Widget / etc.. Unfortunately if I place more widgets, it just shows
   same 2 posts. It was all fine as long as the code was on one page but it stopped
   working when I moved it to widgets.
 * I use $donotduplicate array which is then added to WP_Query as post__not_in. 
   I also tried to put global $donotduplicate to functions.php or front-page.php
   but that didn’t work either.
 * Code can be checked at: [http://pastebin.com/fgXpAnv7](http://pastebin.com/fgXpAnv7)

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5749831)
 * Hi iGreenie
 * It seems the global $post object is not set. Use setup_postdata() to also use
   functions as get_template_part().
    [http://codex.wordpress.org/Function_Reference/setup_postdata](http://codex.wordpress.org/Function_Reference/setup_postdata)
 * Try it with this: [http://pastebin.com/bNKUTqHD](http://pastebin.com/bNKUTqHD)
 *  Thread Starter [Marek Železný](https://wordpress.org/support/users/igreenie/)
 * (@igreenie)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5749847)
 * Oh thanks. I just tried to add global $post and it’s now working. 🙂
    Thanks!
 *  Thread Starter [Marek Železný](https://wordpress.org/support/users/igreenie/)
 * (@igreenie)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5750049)
 * I hope it’s not bad to reopen the topic but I encountered the same problem again,
   yet I can’t get it resolved even with setup_postdata().
 * I have another widget to show posts from 1 category in the sidebar, but I want
   to exclude posts that are on the homepage. When I tried to add global $donotduplicate
   to the widget’s function widget(), it didn’t remember the posts added earlier
   in HP news widget and returned 0 on var_dump.
 * Here is code: [http://pastebin.com/1pbbh2F5](http://pastebin.com/1pbbh2F5)
    It’s
   working in class “mz_hp_otvirak”, “mz_hp_single_news” and I also want to use 
   it in “mz_fcm_komentare_widget” widget.
 * Can someone help me with that?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5750050)
 * Another plugin could alter the $donotduplicate variabe. Rename it to something
   else.
 * Do you set it to 0 anywhere else?
 *  Thread Starter [Marek Železný](https://wordpress.org/support/users/igreenie/)
 * (@igreenie)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5750051)
 * Good point keesiemeijer. I changed the variable from $donotduplicate to $fcm_hp_duplicate_news
   but unfortunately that didn’t help either.
 * I have this variable only in the parts as shown in the pastebin, plus in functions.
   php
 *     ```
       function mz_global_variable() {
           global $donotduplicate;
           $donotduplicate = array();
   
           global $fcm_hp_duplicate_news;
           $fcm_hp_duplicate_news = array();
       }
       add_action( 'init', 'mz_global_variable' );
       ```
   

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

The topic ‘Widgets & WP Query & duplicate posts’ is closed to new replies.

## Tags

 * [post__not_in](https://wordpress.org/support/topic-tag/post__not_in/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)
 * [wp-query](https://wordpress.org/support/topic-tag/wp-query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [Marek Železný](https://wordpress.org/support/users/igreenie/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/widgets-wp-query-duplicate-posts/#post-5750051)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
