Using the Right Query
For all types listing templates, FacetWP will automatically try to find the right query. This usually works great, but sometimes FacetWP needs some extra guidance on which query to use.
The WordPress loop
FacetWP tries to automatically add a facetwp-template
CSS class to an element surrounding the Loop. But due to a myriad of circumstances, sometimes it needs to be done manually.
If so, wrap the CSS class around both the if ( have_posts() )
and else
statements, as shown below).
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
?> <div class="facetwp-template"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', 'post' ); ?> <?php endwhile; else : ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> </div>
The file to edit depends on several factors. As a general rule:
Search page: search.php
Main archive: archive.php
Category archive: category.php
Blog home: home.php
WooCommerce shop: archive-product.php
See WordPress’ built-in Template Hierarchy.
No results
FacetWP was unable to auto-detect the post listing
If you’re seeing this error it’s because FacetWP can’t find a suitable query. This usually happens when you have a custom WP_Query on a standard WordPress page. To let FacetWP detect the custom query, you have to add facetwp => true
to the query arguments. See this section for more information.
This error can also be happen if you are using WP Engine or the WP External Links plugin.
Turn on Debug Mode
If FacetWP isn’t filtering correctly, it’s because FacetWP is using the wrong query.
To find which query FacetWP is using, go to Settings > FacetWP > Settings
, and turn on Debug Mode.
Finding the query
To find the query FacetWP is using, browse to your facet listing, open your browser’s code inspector, go to the Console tab, and type in:
How to use custom JavaScript code?
JavaScript code can be placed in your (child) theme's main JavaScript file. Alternatively, you can add it manually between
<script>
tags in the<head>
section of your (child) theme's header.php file. You can also load it with a hook in your (child) theme's functions.php file, or in the Custom Hooks add-on. To load the code only on pages with facets, use thefacetwp_scripts
hook. To load it on all pages, usewp_head
orwp_footer
. Or you can use a code snippets plugin. More infoFWP.settings.debug;
This will return lots of debugging data, including the query arguments and SQL.
Ignore irrelevant queries
Let’s say you’ve determine that FacetWP is incorrectly using a query with post_type
= edd_wish_list
. We’ll just tell FacetWP to ignore the query, using the facetwp_is_main_query filter:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) { if ( 'edd_wish_list' == $query->get( 'post_type' ) ) { $is_main_query = false; } return $is_main_query; }, 10, 2 );