[wp-hackers] Filters on get_posts()

Casey Bisson casey.bisson at gmail.com
Mon Oct 11 12:02:28 UTC 2010


I think your approach here is good. That is, there's no reason a filter has to be blindly activated as WP is instantiated, and the globally applied.

Filters such as you describe should look at the context of the query and optionally activate themselves. Then immediately deactivate once their work is done. The parse-query and pre-get-posts actions are good places to apply filters, then simply remove_filter() as you run them. 

If you're running multiple loops, you can do yourself a favor by calling do_action('my_loop_name') just before calling wp_query(), as that will give you an opportunity to set whatever filters you want based on that specific loop/query. 



On Oct 10, 2010, at 9:14 PM, John Blackbourn <johnbillion+wp at gmail.com> wrote:

> 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).

More information about the wp-hackers mailing list