<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MultiNC &#187; WordPress dev</title>
	<atom:link href="http://multinc.com/category/wordpress-dev/feed/" rel="self" type="application/rss+xml" />
	<link>http://multinc.com</link>
	<description>Explorations in Social Computing</description>
	<lastBuildDate>Wed, 09 Dec 2009 19:05:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to filter posts from WordPress archive listings</title>
		<link>http://multinc.com/2008/08/27/how-to-filter-posts-from-the-wordpress-archives/</link>
		<comments>http://multinc.com/2008/08/27/how-to-filter-posts-from-the-wordpress-archives/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 10:53:19 +0000</pubDate>
		<dc:creator>huy</dc:creator>
				<category><![CDATA[WordPress dev]]></category>
		<category><![CDATA[WordPress API]]></category>
		<category><![CDATA[WordPress plugin]]></category>

		<guid isPermaLink="false">http://multinc.com/wp/?p=71</guid>
		<description><![CDATA[When developing our Social Privacy plugin, we needed to be able to prevent posts from being listed in the archives, which may be displayed on a blog as "archives by month" or "recently written" listing.  Unfortunately, this useful capability to filter out posts from archives is undocumented but is in fact part of the WordPress plugin API, as we found out from reading the WordPress source code.

Here's how to make use of the archives filter capability.]]></description>
			<content:encoded><![CDATA[<p>When developing our <a title="Social Privacy is a set of open-source plugins for WordPress that restrict the read access of posts or categories to only specified registered users." href="/wp/social-privacy/">Social Privacy</a> plugin, we needed to be able to prevent posts from being mentioned in archive listings, which may be displayed on a blog as &#8220;Archives by month&#8221; or &#8220;Recently written&#8221;.  Unfortunately, this useful capability to filter out posts from archive listings is <em>undocumented </em>but is in fact part of the WordPress plugin API, as we found out from reading the WordPress source code.</p>
<p>Notice that we&#8217;re not talking about preventing the content of posts from displayed in the main part of the page&#8211;that&#8217;s already taken care of by other well-documented API functions.  We&#8217;re talking about not &#8220;leaking&#8221; information about the existence of hidden posts, such as a title that shows up in the &#8220;Recently written&#8221; listing or an otherwise-empty &#8220;August 2008&#8243; that shows up in &#8220;Archives by month&#8221;.</p>
<p>Here&#8217;s how to make use of undocumented WordPress API functions to completely filter out posts from archive listings.</p>
<p>First, create your filter method.  For example,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Filter out protected posts from the archives</span>
<span style="color: #000000; font-weight: bold;">function</span> filter_getarchives_where<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$current_user</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// If the admin does not want to hide any posts, then return the original SQL</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Social_Access_Control_protection_enabled'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// otherwise, modify the SQL with some constraints before returning it</span>
  <span style="color: #000088;">$visible_posts</span> <span style="color: #339933;">=</span> social_access_control<span style="color: #339933;">::</span><span style="color: #004000;">get_posts_visible_to_user</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sql</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; AND ID IN (&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$visible_posts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, just register the filter with a call to add_filter, such as this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'getarchives_where'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'social_access_control'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_getarchives_where'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>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&#8217; filters.</p>
]]></content:encoded>
			<wfw:commentRss>http://multinc.com/2008/08/27/how-to-filter-posts-from-the-wordpress-archives/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
