1

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?

1 Answer 1

1

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

5
  • 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
    Commented Mar 13, 2013 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
    Commented Mar 13, 2013 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. Commented Mar 13, 2013 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
    Commented Mar 13, 2013 at 19:12
  • Yup, just add another condition if(defined('DOING_AJAX') && DOING_AJAX). This one is true only in ajax requests Commented Mar 13, 2013 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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