→ 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.

Usage

Let’s say we have a custom WP_Query on a page that FacetWP isn’t able to auto-detect.

<?php
$args = array(
    'post_type' => 'event',
    'posts_per_page' => 10,
    'facetwp' => true, // we added this
);
$query = new WP_Query( $args );

Notice the custom “facetwp” argument above. Below, we’ll tell FacetWP that if this argument is present, then use the query. This code goes into functions.php.

<?php
function my_facetwp_is_main_query( $is_main_query, $query ) {
    if ( isset( $query->query_vars['facetwp'] ) ) {
        $is_main_query = (bool) $this->query_vars['facetwp'];
    }
    return $is_main_query;
}
add_filter( 'facetwp_is_main_query', 'my_facetwp_is_main_query', 10, 2 );