Overcoming performance issues with posts__not_in with Unique_WP_Query
When developing a WordPress theme, we often want to ensure that there are no duplicates of the same post displayed. For example, if your site has multiple modules (or components, or widgets, or whatever you want to call it), how can we make sure the WP_Query skips over content that has already been displayed in an earlier module?
Well, thankfully WP_Query has a posts__not_in parameter. Unfortunately, it’s not always a great solution. As WordPress VIP outlines, there’s a handful of issues with posts__not_in.
Taking their example solution to the next level, I wrote Unique_WP_Query. Simply replace your WP_Query with Unique_WP_Query and the posts returned will never duplicate.
Check out the Gist below. And if you have any suggestions or improvements, make sure to leave a comment!