11<?php
2- /**
3- * @package Unique_WP_Query
4- * @version 1.0.0
5- */
62/*
73Plugin Name: Unique WP Query
84Plugin URI: https://github.com/firstandthird/unique_wp_query
95Description: A plugin for doing unique WP_Query
106Author: firstandthird
117Version: 1.0.0
12- */
13- // adapted & taken from from https://gist.github.com/jameswburke/d0776d742ab74c469cd8af8dacd916fc
14-
15- /**
16- * Wrapper for WP_Query
17- * Interacts directly with WP_Query_Manager to ensure
18- * only unique posts are ever returned.
19- *
20- * Known issues:
21- * Won't work with offset values
8+ // adapted & taken from https://gist.github.com/jameswburke/d0776d742ab74c469cd8af8dacd916fc
229 */
23- class Unique_WP_Query extends WP_Query {
10+
11+ class Unique_WP_Query extends WP_Query {
2412 function __construct ($ args ) {
2513 // Act as a flag for pre_get_posts
2614 $ args ['unique_wp_query ' ] = true ;
@@ -30,11 +18,14 @@ function __construct($args) {
3018 foreach ($ args ['post__not_in ' ] as $ key => $ id ) {
3119 Unique_WP_Query_Manager::add_post_id ($ id );
3220 }
21+ unset($ args ['post__not_in ' ]);
3322 }
3423
3524 // don't get the current post or page:
36- $ id = get_the_ID ();
37- Unique_WP_Query_Manager::add_post_id ($ id );
25+ if (is_single ()) {
26+ $ id = get_the_ID ();
27+ Unique_WP_Query_Manager::add_post_id ($ id );
28+ }
3829
3930 // Initialize the WP_Query object like normal
4031 parent ::__construct ($ args );
@@ -145,10 +136,9 @@ public static function process_posts(&$posts, &$post_count, $original_posts_per_
145136 function unique_wp_query_pre_get_posts (&$ query ) {
146137 if (true === $ query ->get ('unique_wp_query ' )) {
147138 $ posts_per_page = $ query ->get ('posts_per_page ' );
148-
149- // Increase posts_per_page by the amount of used post_ids
150- $ query ->set ('original_posts_per_page ' , min ($ posts_per_page , 200 ));
151- $ query ->set ('posts_per_page ' , min ($ posts_per_page + Unique_WP_Query_Manager::$ used_post_count , 200 ));
139+ // Increase posts_per_page by the amount of used post_ids
140+ $ query ->set ('original_posts_per_page ' , min ($ posts_per_page , 200 ));
141+ $ query ->set ('posts_per_page ' , min ($ posts_per_page + Unique_WP_Query_Manager::$ used_post_count , 200 ));
152142 }
153143 }
154144
0 commit comments