Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've added a custom filter function for the_posts filter in a plugin.

add_filter('the_posts', 'posts_filter');
function posts_filter() { … }

This works pretty well in the main loop, that means the posts are filtered in the way I defined in the posts_filter function.

But I am calling get_posts() within an ajax request to get some posts. And there the filters aren't working. query_posts() or custom wp_query don't work too.

So the question is: How can I get posts beside the main loop which are filtered by the_posts filter?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

the_posts does work for all queries including the main query as well as custom queries but doesn't work when using get_posts(). This is because get_posts() automatically suppresses all the filters. If you want to use the filters even when using get_posts, you can pass an extra key 'suppress_filters' => false in the array you pass as argument to get_posts()

share|improve this answer
    
Hi Mridul. I thought so too. But for me it isn´t working in this case (not with custom queries and not with get_posts). –  Status4 Mar 13 '13 at 18:42
    
Could the problem be that my filter is set in a plugin? When I set a second filter in the functions.php file of my theme this filter is used correctly. –  Status4 Mar 13 '13 at 18:48
    
plugin or theme shouldn't be a problem. Did you use is_admin() condition in plugin? It returns true for all ajax requests even if they come from frontend. –  Mridul Aggarwal Mar 13 '13 at 18:57
    
Yes I do. You´re right. Thanks! But I need to not filter posts in the admin area. Any idea on this? –  Status4 Mar 13 '13 at 19:12
    
Yup, just add another condition if(defined('DOING_AJAX') && DOING_AJAX). This one is true only in ajax requests –  Mridul Aggarwal Mar 13 '13 at 19:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.