Ready to get a little nerdy?
As we’ve talked about so many times before, WordPress is great because of its extensibility – with the community of developers creating plugins for nearly every possible need, if you dream it, it really can come true.
Our plugin philosophy is always minimalist – don’t use more than you need to, and if you can achieve your goal without adding an additional plugin, don’t add it.
Because there are so many plugins available, they don’t always play nicely with each other. But if you’re using mainstream plugins from trusted developers, then there’s a good chance your plugin knows about (and has work-arounds for) compatibility issues with other popular plugins.
Today, let’s talk about two of our most trusted plugins: Advanced Custom Fields and Relevanssi.
What makes Advanced Custom Fields (ACF) so powerful is that it allows us to store content and page settings in a targeted, unique way for every website – rather than relying on the one-size-fits-all WYSIWYG WordPress editor for all of your content.
Instead of dropping content into a never-ending text box – where it’s difficult to separate out text from images and stylized links – and relying on templates to control every aspect of a page’s design, ACF uses WordPress’s built-in custom fields to store very specific types of information.
That information could be a page’s hero image; it could be the alignment of a three-column row; it could be a file from the Media Gallery; it could be an internal page link – or almost any other piece of information. Because we know what type of data is being stored, we can use it in a much more effective way.
It’s incredibly powerful, and allows us to build websites that provide flexibility as well as tight brand control.
One issue with storing all this data, however, is when it starts to be displayed in places where we don’t want it to show.
We’ve previously shared our love for Relevanssi, the plugin that greatly improves upon the built-in WordPress search. Again, rather than a catch-all, generic search function, Relevanssi and its Premium version allow for greater customization and tweaking.
Settings in Relevanssi include changing the index status of custom post types, adjusting the weighting of content, and – the important piece here – adding custom fields to your search results.
While the basic WordPress search function traditionally only indexes WYSIWYG (or block editor) content, Relevanssi allows us to include content from custom fields, such as the ones created by ACF, in search results. This is hugely important when we’re storing content in custom fields – without it, our search results would be almost useless.
It sounds like we’ve solved our problem, and our plugins are working happily together … right?
Here’s where we run into a common issue: sometimes we don’t want all of the custom fields to be indexed.
In a perfect world, the only custom fields we want to show in search results are ones that have actual text content – something that you can read and understand.
But we’re usually saving more than that with ACF. We’re oftentimes saving things like the background color of a section, or the order of a two-column layout, or the ID of an image or file to link to. That’s information that will get indexed by Relevanssi just like any other custom field and, if for some reason it’s relevant to what is being searched for, it’ll show up in search results.
What does that look like in practice? Here’s an example.
Yikes! You can see that we have some actual, useful text in this result – but also some unhelpful, out-of-context data. Showing “imgleft” doesn’t help the user at all. What exactly is 8436 and why is it displaying here?
Well, these are custom fields with values that are getting swept up in/around the search results Relevanssi wants to display. But they’re not helpful to the user, and in fact make things more confusing.
So how can we handle this?
Thankfully, because it’s from a great developer, Relevanssi has created filters to allow for targeted de-indexing of certain custom fields by name. Here’s how it works.
add_filter( 'relevanssi_index_custom_fields', 'rlv_remove_unwanted' );
function rlv_remove_unwanted( $fields ) {
$unwanted_fields[] = 'sections';
$unwanted_fields[] = 'hero_image';
return array_diff( $fields, $unwanted_fields );
}
Using the relevanssi_index_custom_fields
filter, we add the names of the custom fields that we do not want included in search results to an array; in this case, we’re removing our “sections” custom field, which outlines and orders the different sections used in our Page Builder, and our “hero_image” field, which saves the ID and URL of the hero image being displayed on the page.
If you’re using a lot of custom fields, which we often are, it can be a little cumbersome to do this – but it’s worth it. Thankfully, Relevanssi also has a function to exclude fields based on partial name. From their documentation:
add_filter( 'relevanssi_index_custom_fields', 'rlv_no_layout_fields' );
function rlv_no_layout_fields( $custom_fields ) {
return array_filter(
$custom_fields,
function( $field ) {
return '_layout' !== substr( $field, -7 );
}
);
}
Essentially, anything ending in the world “layout” will automatically be excluded from your search results.
It’s incredibly impressive how much thoughtfulness the Relevanssi team has put into making sure your search results are as targeted and useful as they can be. It’s another reason we’re happy to use and support their work.
If you’d like to learn more about improving your website’s search results, reach out and let’s see what we can do.