[wp-hackers] Filters on get_posts()

Mike Schinkel mikeschinkel at newclarity.net
Mon Oct 11 05:30:23 UTC 2010


John,

I have run into pretty much the same issues.  That's why I wrote this ticket:

http://core.trac.wordpress.org/ticket/15063

The solution proposed in ticket may or may not be good but the problem it was trying to address is real.  

-Mike

On Oct 11, 2010, at 12:14 AM, John Blackbourn wrote:

> These days I find myself using multiple additional loops on the
> majority of sites I build, as I'm sure many people on this list do.
> The problem I'm now running into is the all-or-nothing filtering on
> the get_posts() function which is used for additional loops.
> 
> By default, get_posts() sets the 'suppress_filters' parameter to
> 'true' meaning all the filters such as 'posts_where', 'posts_join' and
> 'posts_request' are not applied. This is desired behaviour, because
> plugins that use these filters often filter the query based on the
> global conditionals and the main WordPress query. If these filters
> were applied to additional loops then we'd probably get unexpected
> results.
> 
> So the problem is that with 'suppress_filters' set to 'true', we're
> left with absolutely no filters on get_posts(). (Well, we have the
> 'posts_search' filter that affects the query if we're doing a search,
> but that's it.)
> 
> I'd like to use get_posts() to get posts for my additional loop, but
> I'd also like to filter the query string so I can join an additional
> table (specifically, the translations lookup table from WPML [1] in
> order to fetch translated posts). The only filter I can use is the
> all-encompassing 'query' filter which applied to every single database
> query.
> 
> In order to get around this I'm currently adding a filter to 'query'
> before calling get_posts() and removing the filter immediately
> afterwards. This is not good for my OCD. I also contemplated fetching
> the posts and then fetching the translated posts in a second query,
> but this isn't ideal either (especially as WPML is a bit heavy on
> queries anyway).
> 
> Is there an argument here for adding a filter to the get_posts() query
> string that's applied regardless of the 'suppress_filters' argument?
> Is that just asking for trouble from plugins that end up incorrectly
> using this filter? Or is there a better way of going about this?
> 
> Regards,
> John
> 
> P.S. I'm aware of the 'pre_get_posts' action that affects the query
> object in this function, but this only lets you control the query
> arguments used to build the query string, not the query string itself.
> 
> [1] http://wpml.org/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list