SearchWP is an advanced search plugin that greatly improves WP core search.

FacetWP includes built-in integration with SearchWP.

Buy SearchWP

Why SearchWP?

By default, WP core search is very basic. It searches only titles and the content field for matches, and has no understanding of relevancy.

SearchWP returns relevant results. It’s also highly configurable, allowing you to search through custom fields, post data, taxonomies, eCommerce data, and even PDFs.

SearchWP also supports custom engines, essentially letting you create different searches to include subsets of data. This is super helpful when combined with a Search facet (so you can search specific fields).

SearchWP settings
The SearchWP settings interface

Usage

FacetWP includes built-in integration with SearchWP. When SearchWP is installed, your Search facets will have additional options under the “Search Engine” setting. Simply choose the desired SearchWP search engine from the dropdown.

SearchWP limits

For large sites, keep in mind that SearchWP has a much lower maximum post limit than FacetWP itself because all the advanced features and extra calculations SearchWP is doing behind the scenes require more server resources.

SearchWP works best on a site with anywhere from a few posts to a few thousand. If your website has tens of thousands of posts or even hundreds of thousands, SearchWP does recommend not using it (see their FAQ).

Remove the post limit

By default, FacetWP fetches a maximum of 200 results from SearchWP. To remove this limit, use the following code:

SearchWP 4.x

<?php
add_filter( 'searchwp\swp_query\args', function( $args ) {
    if ( isset( $args['facetwp'] ) ) {
        $args['posts_per_page'] = -1;
    }
    return $args;
} );

SearchWP 3.x

<?php
add_filter( 'searchwp_swp_query_args', function( $args ) {
    if ( isset( $args['facetwp'] ) ) {
        $args['posts_per_page'] = -1;
    }
    return $args;
} );

Fix “no results” on the search results page

If you are using FacetWP together with SearchWP, and your search results page (the page with /?s=searchterm in the URL) does not have facets, the search will not work properly and show no results.

This is caused by FacetWP’s built-in integration having to intercept the search query before it can know if facets are being used on the page. To fix this issue, you have to disable FacetWP for the search results page only, by adding the following code to your (child) theme’s functions.php:

<?php
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
  if ( $query->is_search() && $query->is_main_query() ) {
    $is_main_query = false;
  }
  return $is_main_query;
}, 5, 2 );

Using SearchWP on the WooCommerce search results page

If you are using FacetWP together with SearchWP and WooCommerce, the WooCommerce search results page will not work well out of the box. WooCommerce uses a special post type archive template for its search results page, which has the “product” post type in the URL, like this:

/?s=searchterm&post_type=product

This search results template behaves differently from the normal WP results page (which does not have &post_type=product in the URL).

The WooCommerce Integration SearchWP extension needs to be installed for SearchWP (and FacetWP) to work well together on this WooCommerce search results page. (It requires a SearchWP Pro license).

Without this extension active, when FacetWP is installed, a WooCommerce search will return no posts for keywords that are indexed by SearchWP, like keywords in custom fields, partial matches etc. Confusingly, searching for keywords that return results in a normal WP search, like keywords from post titles, will work okay.

Note that if you are not using the built-in WooCommerce search results page, the extension is not needed. You can use WP’s normal search results page (without &post_type=product in the URL) with FacetWP and SearchWP without issues.

Using SearchWP with WPML or Polylang

If you are using SearchWP together with WPML or Polylang, make sure to:

  1. Install the Multilingual add-on
  2. Install the appropriate SearchWP integration extension:

The above SearchWP extensions will limit search results to the active language of the page. Without them, there will be a disconnect between the facets, their counts and the post listing.

Debugging SearchWP

SearchWP has its own debugging feature, which can be helpful to gain insight into exactly what SearchWP is doing. The “Debugging enabled” setting can be enable in: Settings > SearchWP > Advanced > Actions & Settings.

When debugging is enabled, a log file is created (one per day) in ~/wp-content/uploads/searchwp-logs/. It shows information about what SearchWP is doing as it indexes and performs searches.

Additionally, an HTML comment block will be output in the source of search results pages, showing information about searches that ran for that page’s request.

See also