When developing our Social Privacy plugin, we needed to be able to prevent posts from being mentioned in archive listings, which may be displayed on a blog as “Archives by month” or “Recently written”. Unfortunately, this useful capability to filter out posts from archive listings is undocumented but is in fact part of the WordPress plugin API, as we found out from reading the WordPress source code.
Notice that we’re not talking about preventing the content of posts from displayed in the main part of the page–that’s already taken care of by other well-documented API functions. We’re talking about not “leaking” information about the existence of hidden posts, such as a title that shows up in the “Recently written” listing or an otherwise-empty “August 2008″ that shows up in “Archives by month”.
Here’s how to make use of undocumented WordPress API functions to completely filter out posts from archive listings.
First, create your filter method. For example,
// Filter out protected posts from the archives function filter_getarchives_where($sql) { global $current_user; // If the admin does not want to hide any posts, then return the original SQL if (! get_option('Social_Access_Control_protection_enabled')) return $sql; // otherwise, modify the SQL with some constraints before returning it $visible_posts = social_access_control::get_posts_visible_to_user($current_user); $sql = $sql . " AND ID IN (" . implode(",", $visible_posts) . ")"; return $sql; }
Then, just register the filter with a call to add_filter, such as this example:
add_filter('getarchives_where', array('social_access_control', 'filter_getarchives_where'), 10000);
Since hiding sensitive posts is very important for plugin, we give our filter a very high priority (10000) to make sure that it gets executed last and makes sure that our plugin has an effect that is not overridden by other plugins’ filters.