Stale $GLOBALS[‘post’] after custom WP_Query empty result set bothers my template, whose fault is this?

Story below looks to me like it’s about a commercial Event Management plugin whose authors to me just don’t seem very good at WordPress. But bear with me and let’s see if there’s also an issue in WP. This is on 3.5.2.

Chain of events I’m looking into:

Read More
  • custom taxonomy query is made in browser

  • $wp_the_query gets all results (1 post total), WordPress sets $GLOBALS['post'] to this post

  • in “wp” action, commercial plugin builds new WP_Query (meta query, resulting in a correct EMPTY RESULT SET), then replaces global $wp_query with this empty result by bluntly doing $wp_query = $this->_wp_query;

  • but nobody touches $GLOBALS['post'], so all subsequent get_post() calls see $wp_query->post not being there, fall back on $GLOBALS['post'] and cause incorrect results (post_class() and friends, who think they have an <article> element to work with)

If I manually hook into “wp” action after commercial plugin has finished its’ query and do $GLOBALS['post'] = null, template will correctly reflect reality.

  • we have even more fun when for example Yoast’s SEO plugin does wp_reset_query() for its own magic in “wp_head” action, which will resurrect global $post variable from $wp_the_query after I’ve nulled it earlier in “wp”

So this is some sort of an issue with empty result set custom queries. I have not see any guidelines so far to care for resetting global $post variable like this. While the plugin is just badly built, should there be something for this addressed in core?

Related posts