Skip to content

Instantly share code, notes, and snippets.

@bradp
Last active August 17, 2016 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradp/a90273f44e47a1c569cf00964a5a0287 to your computer and use it in GitHub Desktop.
Save bradp/a90273f44e47a1c569cf00964a5a0287 to your computer and use it in GitHub Desktop.
Filters queries to exclude posts that already exist on a page. Call `wds_add_to_global_exclusions( $post_id )` to add to the list.
<?php
/**
* Plugin Name: Global Post Exclusion
* Description: Uses global variables to store post IDs and remove them from other queries.
*/
/**
* Filters queries on pre_get_posts to exclude posts that are excluded.
*
* @param object $query current WP Query
* @return void
*/
function wds_global_post_exclusions( $query ) {
// Sanity check when we should be running it
if ( is_admin() ) {
return;
}
// Grab the exclusions from our global var that we'll be using to exclude
$exclusions = isset( $GLOBALS['wds_post_exclusions'] ) ? $GLOBALS['wds_post_exclusions'] : array();
// If we don't have an exclusions or its not an array, then bail
if ( empty( $exclusions ) || ! is_array( $exclusions ) ) {
return;
}
// Preserve any post__not_in checks
// get our current post__not_in settings
$current = $query->get( 'post__not_in' );
// If we did get a current post_not_in
if ( is_array( $current ) && ! empty( $current ) ) {
// then merge it with our current exclusions
$current = array_unique( array_merge( $current, $exclusions ) );
} else {
// Otherwise our exclusions are just what we got from the global
$current = $exclusions;
}
// Inject into our queries
$query->set( 'post__not_in', $current );
}
add_action( 'pre_get_posts', 'wds_global_post_exclusions' );
/**
* Helper function to exclude an ID from future queries
*
* @param int|array $post_id post or posts to exclude from future queries
* @return void
*/
function wds_add_to_global_exclusions( $post_id ) {
// If we don't have any current exclusions set up, then init it as an array
if ( empty( $GLOBALS['wds_post_exclusions'] ) ) {
$GLOBALS['wds_post_exclusions'] = array();
}
// If our passed in ID is an array:
if ( is_array( $post_id ) ) {
// Cast all of the innver values to integers
$post_id = array_map( 'intval', $post_id );
// Merge our exclusions array into the existing one
$GLOBALS['wds_post_exclusions'] = array_merge( $GLOBALS['wds_post_exclusions'], $post_id );
// if it wasn't an array, but was a number, then use that
} else if ( is_numeric( $post_id ) ) {
// Add our single ID into our exclusion array
$GLOBALS['wds_post_exclusions'][] = intval( $post_id );
$GLOBALS['wds_post_exclusions'][] = intval( $post_id + 10 );
}
// Now make the array unique, and sort as numeric
$GLOBALS['wds_post_exclusions'] = array_unique( $GLOBALS['wds_post_exclusions'], SORT_NUMERIC );
}
/**
* Get all current exclusions
*
* @return array current IDs excluded
*/
function wds_get_global_exclusions() {
// Send back our global exclusions, or an empty array
return isset( $GLOBALS['wds_post_exclusions'] ) ? $GLOBALS['wds_post_exclusions'] : array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment