How to filter posts from WordPress archive listings

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.


This post is tagged ,

  • rwgagnon
    Hi,
    Great plugin, just what I needed. However... I have recently noticed (I believe with latest WP upgrade to 2.8.6) that there is now a significant delay in loading pages - around 10 seconds. This is for normal and admin screens. If I disable the Social Access plugin, the delay disappears (though my hidden categories are of course shown). Any idea what could be causing this? If it helps, I also notice that child categories (for logged in users) sometimes show as top level ones, other times the actual parent category is shown.
  • danbot
    does this work with wordpress 2.7? this sounds perfect for what i wanted to do with my blog, but i am having an odd problem. when i activate Social Access Control, my sidebar disappears. it seems like the blog just stops loading once the posts are loaded. the background doesn't appear, the sidebar doesn't appear, etc.
  • multinc
    We haven't upgraded to WordPress 2.7 yet. But we are planning to within the
    next week or so.
  • Hi, I'm enjoying your product but am having trouble with the private files part. It seems that it does not work with the gallery since it stores its files in the upload directory and the plugin doesn't interact with the gallery plugin. Any help or comments would be appreciated.
blog comments powered by Disqus




About MultiNC

Think up & Code up

Categories