TransWikia.com

How to reset/remove added filters 'posts_join' and 'posts_orderby' after the loop is completed?

WordPress Development Asked by Debiprasad on January 23, 2021

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?

One Answer

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.

Answered by Aurovrata on January 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP