facetwp_is_main_query
Overview
This filter is only relevant for the CSS-based template approach. This hook is useful if FacetWP is unable to auto-detect the correct query (such as when there’s a custom query on a page).
Parameters
- $is_main_query | boolean | Whether FacetWP should use the current query
- $query | object | The WP_Query object
How it works
When using the CSS-based template approach, FacetWP will automatically try to find the right query. FacetWP uses this logic to determine whether a query should be used:
$is_main_query = ( $query->is_archive || $query->is_search || ( $query->is_main_query() && ! $query->is_singular ) );
FacetWP uses the first query that matches the above criteria. While this works great most of the time, we understand that this won’t work 100% of the time with all themes. This hook is useful when the above logic isn’t sufficient.
Forcing a custom query
Let’s say we have a custom WP_Query on a page that FacetWP isn’t able to auto-detect.
$args = [
'post_type' => 'event',
'posts_per_page' => 10,
'facetwp' => true, // we added this
];
$query = new WP_Query( $args );
By setting the “facetwp” argument (see above), FacetWP will use the query for filtering unless another query has already been chosen.
Archive queries
On archive pages (such as category or /shop/
archives), FacetWP will prioritize the main archive query unless explicitly told otherwise. FacetWP shortcode templates are no exception.
To force FacetWP to ignore archive queries, see the following example:
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( $query->is_archive() && $query->is_main_query() ) {
$is_main_query = false;
}
return $is_main_query;
}, 10, 2 );
Usage
This example tells FacetWP to ignore queries with post_type set to tribe_events
:
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( 'tribe_events' == $query->get( 'post_type' ) ) {
$is_main_query = false;
}
return $is_main_query;
}, 10, 2 );