Greetings,
A site I'm developing has a custom post type (CPT) "events" with associated post metadata, taxonomy, and taxonomy metadata (via the
Taxonomy Metadata plugin). Because of all this taxonomy and metadata to filter on, plus a possible core bug I might have found but will investigate later, I needed to create a custom query for WP_Query. I do this with the following code in my plugin:
if( !is_admin() ) :
add_filter( 'posts_join', 'afg_postsJoin' );
add_filter( 'posts_where', 'afg_postsWhere' );
add_filter( 'posts_groupby', 'afg_postsGroupBy' );
add_filter( 'posts_orderby', 'afg_postsOrderBy' );
endif;
Each function called by the filters checks to see if the global $wp_query->query_vars[ 'post_type' ] === "events" and filters each section of the query if it is.
It works perfectly on single and archive pages for that CPT. It would also work in the admin pages if I didn't have the !is_admin() check in place.
But it's not working in a widget. In my widget I'm setting up a new WP_Query instance with the following arguments:
$eventsQuery = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => $eventsToDisplay, // from widget settings
'ignore_sticky_posts' => $ignoreSticky // from widget settings
) );
I am calling wp_reset_postdata() both before this code is run and after I am finished with this secondary loop.
I would have assumed, since I set the post_type to "events" in the args, that the posts_join and other filters would fire. But it appears they are not. The only place it actually works is if I have the widget in a sidebar on the /events/ archive page or a single events page. In other words, the filters only fire in the widget on pages where "events" is the post type of the main loop.
Does anyone know how I can get those filters to function in the widget on all pages, not just pages associated with the CPT?
Thanks everyone!