0

I am sorting results appear on the category archive page using these filters: posts_join and posts_orderby. There is a widget, which displays recent posts. I am using default recent posts widget of WordPress. But it's not showing any result. When I investigated I found that the applied filters are getting applied to this query. So, I tried remove the filters using the following code:

remove_filter('posts_join', 'my_filter_join');
remove_filter('posts_orderby', 'my_filter_orderby');

But still it does not work.

I also tried to put the following code in the filter functions itself:

// Disable this filter for future queries!
remove_filter(current_filter(), __FUNCTION__);

It does not work either.

How to fix this issue?

3
  • What priority are you assigning to your filters with add_filter()? If it's different than the default, then you need to use the same priority with remove_filter(). So, for example, if you have add_filter( 'posts_join', 'my_filter_join', 20 );, you would need to have remove_filter( 'posts_join', 'my_filter_join', 20 ); to remove it.
    – Pat J
    Sep 11, 2013 at 13:16
  • I am using default priority. For example, add_filter('posts_join', 'my_filter_join');.
    – Debiprasad
    Sep 11, 2013 at 13:39
  • In principle, what you are doing looks right. Post your code in context please, so we can see how it is actually being used.
    – s_ha_dum
    Sep 11, 2013 at 14:00

1 Answer 1

0

There is a couple of ways to achieve this,

  1. use a custom query for your widget using get_posts function where the query attribute suppress_filters is set to false (the default). This would ensure your filters are not fired for this query.
  2. add better validation to your filter to figure what is the query on which to apply the filter and else return. Use WP conditional tags to test for the page you're on (category archive page) as opposed to a sidebar/widget query.

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.