|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Unique WP Query |
| 4 | +Plugin URI: https://github.com/firstandthird/unique_wp_query |
| 5 | +Description: A plugin for doing unique WP_Query |
| 6 | +Author: firstandthird |
| 7 | +Version: 1.0.0 |
| 8 | +// adapted & taken from https://gist.github.com/jameswburke/d0776d742ab74c469cd8af8dacd916fc |
| 9 | + */ |
| 10 | + |
| 11 | +class Unique_WP_Query extends WP_Query { |
| 12 | + function __construct($args) { |
| 13 | + // Act as a flag for pre_get_posts |
| 14 | + $args['unique_wp_query'] = true; |
| 15 | + |
| 16 | + // don't get any pages specified by post__not_in: |
| 17 | + if (key_exists( 'post__not_in', $args)) { |
| 18 | + foreach ($args['post__not_in'] as $key => $id) { |
| 19 | + Unique_WP_Query_Manager::add_post_id($id); |
| 20 | + } |
| 21 | + unset($args['post__not_in']); |
| 22 | + } |
| 23 | + |
| 24 | + // don't get the current post or page: |
| 25 | + if (is_single()) { |
| 26 | + $id = get_the_ID(); |
| 27 | + Unique_WP_Query_Manager::add_post_id($id); |
| 28 | + } |
| 29 | + |
| 30 | + // Initialize the WP_Query object like normal |
| 31 | + parent::__construct($args); |
| 32 | + |
| 33 | + // Track used posts, and remove duplicates |
| 34 | + Unique_WP_Query_Manager::process_posts($this->posts, $this->post_count, $this->get('original_posts_per_page')); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Keeps track of which posts have already appeared and |
| 40 | + * removes them from future Unique_WP_Query objects. |
| 41 | + */ |
| 42 | +class Unique_WP_Query_Manager { |
| 43 | + |
| 44 | + /** |
| 45 | + * Keep track of which post_ids have already been used |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + public static $used_post_ids = []; |
| 49 | + |
| 50 | + /** |
| 51 | + * Keeps track of how many used_posts_ids exist |
| 52 | + * @var integer |
| 53 | + */ |
| 54 | + public static $used_post_count = 0; |
| 55 | + |
| 56 | + /** |
| 57 | + * Helper to make sure no duplicate ids are added |
| 58 | + * useful for content outside of the normal query |
| 59 | + */ |
| 60 | + public static function add_post_id($id) { |
| 61 | + if (!in_array($id, Unique_WP_Query_Manager::$used_post_ids, true)) { |
| 62 | + Unique_WP_Query_Manager::$used_post_ids[] = $id; |
| 63 | + Unique_WP_Query_Manager::$used_post_count = count(Unique_WP_Query_Manager::$used_post_ids); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Takes the posts and post count of a WP_Query object and |
| 69 | + * ensures that it removes posts that have already been used, |
| 70 | + * and then trims it to the necessary size. |
| 71 | + * |
| 72 | + * @param array &$posts |
| 73 | + * @param integer &$post_count |
| 74 | + */ |
| 75 | + public static function process_posts(&$posts, &$post_count, $original_posts_per_page) { |
| 76 | + |
| 77 | + // Keep track of how many posts are acceptable to return |
| 78 | + $current_accepted_post_count = 0; |
| 79 | + |
| 80 | + // Make sure we have posts |
| 81 | + if (empty((array) $posts)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Loop through all the found posts |
| 86 | + foreach ((array) $posts as $key => $post) { |
| 87 | + |
| 88 | + // If we have enough acceptable posts, break the loop |
| 89 | + // Otherwise, determine if we've already used this post |
| 90 | + if ($original_posts_per_page > $current_accepted_post_count) { |
| 91 | + |
| 92 | + // Has this post already been used? |
| 93 | + if (in_array($post->ID, Unique_WP_Query_Manager::$used_post_ids, true)) { |
| 94 | + // Remove from $posts |
| 95 | + unset($posts[$key]); |
| 96 | + |
| 97 | + // And update count |
| 98 | + $post_count--; |
| 99 | + } else { |
| 100 | + // If not, add it to the list of used ids |
| 101 | + Unique_WP_Query_Manager::$used_post_ids[] = $post->ID; |
| 102 | + |
| 103 | + // Update how many accepted posts we have |
| 104 | + $current_accepted_post_count++; |
| 105 | + } |
| 106 | + } else { |
| 107 | + // If we have enough acceptable posts, break the foreach |
| 108 | + // This prevents extra results from accidently being added to $used_post_ids |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Reindex the array correctly |
| 114 | + $posts = array_values($posts); |
| 115 | + |
| 116 | + // Update the $used_post_count |
| 117 | + Unique_WP_Query_Manager::$used_post_count = count(Unique_WP_Query_Manager::$used_post_ids); |
| 118 | + |
| 119 | + // Trim any excess posts and update $post_count |
| 120 | + if (count($posts) > $original_posts_per_page) { |
| 121 | + |
| 122 | + // Remove extra posts |
| 123 | + $posts = array_slice($posts, 0, $original_posts_per_page); |
| 124 | + |
| 125 | + // Update post count to our new value |
| 126 | + $post_count = $original_posts_per_page; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +if (!function_exists('unique_wp_query_pre_get_posts')) { |
| 132 | + /** |
| 133 | + * Increase the posts_per_page value by the number of posts that have |
| 134 | + * already been used. Executes as the last pre_get_posts action. |
| 135 | + */ |
| 136 | + function unique_wp_query_pre_get_posts(&$query) { |
| 137 | + if (true === $query->get('unique_wp_query')) { |
| 138 | + $posts_per_page = $query->get('posts_per_page'); |
| 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)); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + add_action('pre_get_posts', 'unique_wp_query_pre_get_posts', PHP_INT_MAX); |
| 146 | +} |
0 commit comments