<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: WordPress: Using Filters With&#160;get_posts()</title>
	<atom:link href="http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/#utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wordpress-using-filters-with-get_posts</link>
	<description>Random stuff written by Alex Mills</description>
	<lastBuildDate>Sat, 11 Feb 2012 01:00:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
	<item>
		<title>By: Derek Perkins</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-235886</link>
		<dc:creator>Derek Perkins</dc:creator>
		<pubDate>Fri, 20 Jan 2012 22:01:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-235886</guid>
		<description>You nailed it. That makes me feel dumb, but thanks a ton for the help.</description>
		<content:encoded><![CDATA[<p>You nailed it. That makes me feel dumb, but thanks a ton for the help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex (Viper007Bond)</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-235885</link>
		<dc:creator>Alex (Viper007Bond)</dc:creator>
		<pubDate>Fri, 20 Jan 2012 20:21:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-235885</guid>
		<description>That should work fine. Does &lt;code&gt;$args&lt;/code&gt; contain &lt;code&gt;suppress_filters&lt;/code&gt;? ;)

[code language=&quot;php&quot;]
// ...

$args = wp_parse_args( $args );
$args[&#039;suppress_filters&#039;] = false;

$posts = get_posts( $args );

// ...
[/code]</description>
		<content:encoded><![CDATA[<p>That should work fine. Does <code>$args</code> contain <code>suppress_filters</code>? <img src='http://www.viper007bond.com/wordpress/wp-content/plugins/tango-smilies/tango/face-wink.png' alt=';)' class='wp-smiley' /> </p>
<pre class="brush: php; title: ; notranslate">
// ...

$args = wp_parse_args( $args );
$args['suppress_filters'] = false;

$posts = get_posts( $args );

// ...
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derek Perkins</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-235884</link>
		<dc:creator>Derek Perkins</dc:creator>
		<pubDate>Fri, 20 Jan 2012 20:16:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-235884</guid>
		<description>I like this code a lot, but I am trying to add the filter inside a function and it doesn&#039;t seem to be working, even if I remove the conditional. Is that something WordPress doesn&#039;t allow for?
[code language=&quot;php&quot;]
function sr_get_submenu( $args, $sr_last_fourteen_days = false, $image_size = &#039;small_thumb&#039; ) {
	$output = &#039;&#039;;
	if($sr_last_fourteen_days) 
	add_filter( &#039;posts_where&#039;, &#039;sr_last_fourteen_days&#039; );
	
	$posts = get_posts( $args );
	........

	if($sr_last_fourteen_days) remove_filter( &#039;posts_where&#039;, &#039;sr_last_14_days&#039; );
	
}

function sr_last_fourteen_days( $where = &#039;&#039; ) {
    global $wpdb;
    $where .= $wpdb-&gt;prepare( &quot; AND post_date &gt; %s&quot;, date( &#039;Y-m-d&#039;, strtotime(&#039;-14 days&#039;) ) ); 
    return $where;
}
[/code]</description>
		<content:encoded><![CDATA[<p>I like this code a lot, but I am trying to add the filter inside a function and it doesn&#8217;t seem to be working, even if I remove the conditional. Is that something WordPress doesn&#8217;t allow for?</p>
<pre class="brush: php; title: ; notranslate">
function sr_get_submenu( $args, $sr_last_fourteen_days = false, $image_size = 'small_thumb' ) {
	$output = '';
	if($sr_last_fourteen_days)
	add_filter( 'posts_where', 'sr_last_fourteen_days' );

	$posts = get_posts( $args );
	........

	if($sr_last_fourteen_days) remove_filter( 'posts_where', 'sr_last_14_days' );

}

function sr_last_fourteen_days( $where = '' ) {
    global $wpdb;
    $where .= $wpdb-&gt;prepare( &quot; AND post_date &gt; %s&quot;, date( 'Y-m-d', strtotime('-14 days') ) );
    return $where;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabriel Dancause</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-235678</link>
		<dc:creator>Gabriel Dancause</dc:creator>
		<pubDate>Tue, 20 Dec 2011 05:19:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-235678</guid>
		<description>I&#039;m trying to get the 6 previous posts in my single.php in relation to the post being displayed. Would you have an idea on how to use your code to achieve that task?</description>
		<content:encoded><![CDATA[<p>I&#8217;m trying to get the 6 previous posts in my single.php in relation to the post being displayed. Would you have an idea on how to use your code to achieve that task?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: WordPress - Showing Ads only on Old Posts</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-233960</link>
		<dc:creator>WordPress - Showing Ads only on Old Posts</dc:creator>
		<pubDate>Thu, 17 Mar 2011 23:37:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-233960</guid>
		<description>[...] to Viper007Bond for, quite literally, showing me how to do [...]</description>
		<content:encoded><![CDATA[<p>[...] to Viper007Bond for, quite literally, showing me how to do [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex (Viper007Bond)</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-233778</link>
		<dc:creator>Alex (Viper007Bond)</dc:creator>
		<pubDate>Fri, 11 Feb 2011 19:11:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-233778</guid>
		<description>Yeah, that&#039;s the best way to do it. Add it, query, and then remove it.</description>
		<content:encoded><![CDATA[<p>Yeah, that&#8217;s the best way to do it. Add it, query, and then remove it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: larf2k</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-233777</link>
		<dc:creator>larf2k</dc:creator>
		<pubDate>Fri, 11 Feb 2011 18:48:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-233777</guid>
		<description>Oh man!  I was pulling hairs out for an hour over this until I found your post.

Thanks so much, this allows for so many possibilities.  Altering the where clause is really powerful. 

Question though: if I only want it to apply to one get_posts() and no others, or, say have different get_posts()&#039;s on different parts of the theme have different where filters, should I do something like:
[php]
function filter_where( $where = &#039;&#039; ) {
	// posts in the last 30 days
	$where .= &quot; AND post_date &gt; &#039;&quot; . date(&#039;Y-m-d&#039;, strtotime(&#039;-60 days&#039;)) . &quot;&#039;&quot;;
	return $where;
}
add_filter( &#039;posts_where&#039;, &#039;filter_where&#039; );
			$posts = get_posts(array(
				&#039;suppress_filters&#039; =&gt; false,
				&#039;numberposts&#039; =&gt; -1,
				&#039;category__not_in&#039; =&gt; explode(&#039;,&#039;,$subscriber-&gt;categories_excluded),
				&#039;post_type&#039; =&gt; $post_types_included
				)
			);
remove_filter( &#039;posts_where&#039;, &#039;filter_where&#039; )
[/php]
(removing the filter so it doesn&#039;t affect other suppress_filter get_posts()&#039;s)

I&#039;m thinking this is necessary otherwise this filter will be carried out sitewide...</description>
		<content:encoded><![CDATA[<p>Oh man!  I was pulling hairs out for an hour over this until I found your post.</p>
<p>Thanks so much, this allows for so many possibilities.  Altering the where clause is really powerful. </p>
<p>Question though: if I only want it to apply to one get_posts() and no others, or, say have different get_posts()&#8217;s on different parts of the theme have different where filters, should I do something like:</p>
<pre class="brush: php; title: ; notranslate">
function filter_where( $where = '' ) {
	// posts in the last 30 days
	$where .= &quot; AND post_date &gt; '&quot; . date('Y-m-d', strtotime('-60 days')) . &quot;'&quot;;
	return $where;
}
add_filter( 'posts_where', 'filter_where' );
			$posts = get_posts(array(
				'suppress_filters' =&gt; false,
				'numberposts' =&gt; -1,
				'category__not_in' =&gt; explode(',',$subscriber-&gt;categories_excluded),
				'post_type' =&gt; $post_types_included
				)
			);
remove_filter( 'posts_where', 'filter_where' )
</pre>
<p>(removing the filter so it doesn&#8217;t affect other suppress_filter get_posts()&#8217;s)</p>
<p>I&#8217;m thinking this is necessary otherwise this filter will be carried out sitewide&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Bailey</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-232400</link>
		<dc:creator>Andy Bailey</dc:creator>
		<pubDate>Sun, 29 Aug 2010 16:20:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-232400</guid>
		<description>this was just enough information to get me in trouble!

I ended up having filters for posts_where, post_limits, posts_orderby, posts_join, posts_groupby all working together in my plugin!

thanks for the post, it really helped!</description>
		<content:encoded><![CDATA[<p>this was just enough information to get me in trouble!</p>
<p>I ended up having filters for posts_where, post_limits, posts_orderby, posts_join, posts_groupby all working together in my plugin!</p>
<p>thanks for the post, it really helped!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viper007Bond</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-231276</link>
		<dc:creator>Viper007Bond</dc:creator>
		<pubDate>Wed, 16 Jun 2010 22:49:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-231276</guid>
		<description>It&#039;s one of the function&#039;s optional arguments. Setting it to &lt;code&gt;false&lt;/code&gt; will disable the function from not using any filters on it&#039;s query and results like it normally does.</description>
		<content:encoded><![CDATA[<p>It&#8217;s one of the function&#8217;s optional arguments. Setting it to <code>false</code> will disable the function from not using any filters on it&#8217;s query and results like it normally does.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mariano</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-231272</link>
		<dc:creator>Mariano</dc:creator>
		<pubDate>Wed, 16 Jun 2010 20:54:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-231272</guid>
		<description>Thanks for the tip :)</description>
		<content:encoded><![CDATA[<p>Thanks for the tip <img src='http://www.viper007bond.com/wordpress/wp-content/plugins/tango-smilies/tango/face-smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: uwiuw</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-230142</link>
		<dc:creator>uwiuw</dc:creator>
		<pubDate>Tue, 16 Mar 2010 07:40:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-230142</guid>
		<description>care to explain more about &#039;suppress_filters&#039; ? what is it ?</description>
		<content:encoded><![CDATA[<p>care to explain more about &#8216;suppress_filters&#8217; ? what is it ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: WordPress Picks for the Week [02/03] &#124; Techtites</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-229773</link>
		<dc:creator>WordPress Picks for the Week [02/03] &#124; Techtites</dc:creator>
		<pubDate>Sat, 06 Feb 2010 10:26:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-229773</guid>
		<description>[...] Using Filters With get_posts() [...]</description>
		<content:encoded><![CDATA[<p>[...] Using Filters With get_posts() [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Gaudet</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-229741</link>
		<dc:creator>Jim Gaudet</dc:creator>
		<pubDate>Wed, 03 Feb 2010 13:29:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-229741</guid>
		<description>Sweet, thanks! I knew it wasn&#039;t right, but it got me thinking of what I wanted to do and then you did it for me,

Much appreciated,</description>
		<content:encoded><![CDATA[<p>Sweet, thanks! I knew it wasn&#8217;t right, but it got me thinking of what I wanted to do and then you did it for me,</p>
<p>Much appreciated,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Viper007Bond</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-229722</link>
		<dc:creator>Viper007Bond</dc:creator>
		<pubDate>Tue, 02 Feb 2010 22:41:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-229722</guid>
		<description>You wouldn&#039;t use this code to do that. You would want something like this:

[php]&lt;?php

if ( get_the_time(&#039;U&#039;) &lt; strtotime(&#039;-30 days&#039;) ) {
	// show ads
}

?&gt;[/php]</description>
		<content:encoded><![CDATA[<p>You wouldn&#8217;t use this code to do that. You would want something like this:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php

if ( get_the_time('U') &lt; strtotime('-30 days') ) {
	// show ads
}

?&gt;</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Gaudet</title>
		<link>http://www.viper007bond.com/2010/02/02/wordpress-using-filters-with-get_posts/comment-page-1/#comment-229720</link>
		<dc:creator>Jim Gaudet</dc:creator>
		<pubDate>Tue, 02 Feb 2010 15:08:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.viper007bond.com/?p=1384#comment-229720</guid>
		<description>This is great, for me I am going to use something like this to only show ads on posts that are of a certain age..</description>
		<content:encoded><![CDATA[<p>This is great, for me I am going to use something like this to only show ads on posts that are of a certain age..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

