WordPress.org

Support

Support » Plugins and Hacks » Hooks/Filters called from the WP_Query class should be passed the instance

Hooks/Filters called from the WP_Query class should be passed the instance

  • I think Hooks/Filters called from the WP_Query class should be passed the instance.

    I was writing a plugin that added some posts to the main query under certain circumstances. However, this was having the side effect of altering the results of widgets such as the Recent Posts widget.

    As it turns out, the Recent Posts widget function creates a new instance of WP_Query. My code was assuming there was only one global instance of WP_Query ($GLOBALS[‘wp_query’]), and was thus using functions like is_home() (which return values from $GLOBALS[‘wp_query’]) inside of my hooks.

    In summary, the ‘the_posts’ filter (and filters/hooks like it) should pass the instance ($this), because any functions which filter on it can’t assume that it is being called using the global instance, and thus can’t rely on functions like is_home(), is_archive(), is_search(), etc.

    Anyone have any ideas for some workarounds to get the current instance when it isn’t passed to me?

  • The topic ‘Hooks/Filters called from the WP_Query class should be passed the instance’ is closed to new replies.