One of the key goals for the redesign was improved site search. Users need to easily find information about the cases they care about.
We leveraged SearchWP for relevant search results, indexing of documents, and generating related content listings throughout the site. We used FacetWP for advanced filtering on the search results page.
Here are some tips and code snippets I used to implement the advanced site search.
We use SearchWP on every site because it provides two valuable features:
- It expands the data used for indexing.
While the standard WordPress search only looks at the title and content fields, SearchWP lets you index metadata, taxonomies, and more. You can also control which content types can be included in the index and the weighting of different factors when determining relevance.
- It delivers relevant search results.
The standard WordPress search finds any post with a keyword match in the title or content and returns those results with the most recent first. The most recent article often isn’t the most relevant.
Include documents in search
When setting up SearchWP, we added “Media” as one of the post types to index, and limited the file type to “All Documents”.
If you check “Transfer weight to parent”, the post or page containing the document will be displayed instead of the document itself, which is a really nice feature.
In this particular case we wanted to expose the documents themselves. You’ll need to ensure your theme displays ‘attachment’ posts correctly in the loop.
Here’s the archive partial we used for documents in search results. We’re also including the source page as a clickable subtitle in the results.
We wanted to provide editors with insight into how users are interacting with the site search. SearchWP Metrics is perfect for this. It lets you monitor the amount of site searches, popular search terms, average ranking of clicked result, and more.
The “No Result Searches” can be a tool to determine new content that needs to be written, updates to existing content so it better matches common search terms, or search synonyms you can add to direct those searches to ones with results.
When you have the SearchWP Related addon installed, it adds a “SearchWP Related Content” metabox to all post types.
The code below makes the following changes:
- Limits it to just the post, case, and press_release post type
- Sets the priority to ‘low’ so it doesn’t appear above important metaboxes
- Changes the title to “Related News”
Add labels to FacetWP filters
Filter by year
When you create a facet, you can select from many different types and data sources. I used the “Slider” type and “Post Date” as the data source.
The Post Date source uses a UNIX timestamp, so I had to customize the indexer to use the year for this facet.
My facet name was
filter_by_year, so change that in the code below to match your facet name. Once you’ve added this code, click “Re-index” to update the data.
Hide empty facets
Some of the filters we’ve added only apply to certain content types. For instance, the “Open” and “Closed” status at the bottom is a taxonomy on the ‘case’ content type.
FacetWP displays the filter regardless of whether there’s anything to filter. The code below hides the facet if it’s empty. Note: this assumes each facet is inside of its own
Handle empty searches
If a user submitted an empty search form, WordPress listed all the site’s content and the filters appeared in the sidebar. Selecting any filter caused the 404 page to load as a listing in the content area.
I added the following to functions.php to redirect empty search queries to a Search page that contained a search form and no FacetWP filters.
pre_get_posts instead of
template_redirect so I can target only the main query. When I used
template_redirect I ran into an issue with FacetWP filters not working.
Customize FacetWP Pagination
[facetwp pager="true"] or the function
facetwp_display( 'pager' );.
It’s a good idea to add pagination scrolling so users are taken to the top of the results after navigating to a new page.
You can customize the markup of the pagination function using the
facetwp_pager_html filter. Here’s what I used on the site referenced above to match our standard pagination used elsewhere:
Include attachments in search results
Even though I had set SearchWP to index attachments, FacetWP wasn’t returning them in the search results. The code below tells FacetWP to include attachments in its query:
List total number of found results
You can use
facetwp_display( 'counts' ); or
[facetwp counts="true"] to display the number of posts matching the current faceted search.
You can customize the output using the
You can display a dropdown to reorder the results using
facetwp_display( 'sort' ); or
The placeholder when nothing has been selected is “Sort by”, which I changed to “Relevance” since SearchWP is delivering relevancy-based results.
Use singular name in post type filter
We have a “Filter by Type” facet for filtering by post type. This uses the post type name which is typically plural, but the client wanted the singular form used in the filter. Also, we wanted to change “Media” to “Document” since that’s the only type of media we’re indexing.
You can use the
facetwp_index_row filter, and then check the facet name before making changes. In my case, our facet name is