WordPress: using Excerpts from Advanced Custom Fields on the Search Results Page

September 14, 2018
Developers, Solutions
LinkedInTwitterFacebookEmail

If you use the Advanced Custom Field’s module system to build the pages in your site, you’ll come across an issue on the search results page: as each page is listed with the excerpt below, nothing will show for your pages that are built with the modules.

Fear not! This can be remedied fairly quickly.

Solutions - Computer with Glasses

Install Relevanssi

I’m sure this can be done with other plugins, but I typically use Relevanssi (despite my repeated failure to spell it correctly) to index the information inside of the Advanced Custom Field’s fields.

Relevanssi can do quite a bit for you. You can set it to index all custom fields (Indexing -> Custom Fields set to Visible) AND you can check the box for using the custom fields to create excerpts. (Excerpts & Highlights -> Use custom fields for excerpts)

But unfortunately, allowing Relevanssi to create the excerpt out of the custom fields can lead to some weird results (see below). I recommend leaving that second one unchecked (use custom fields for excerpts). Otherwise, you might see things like this:

Not exactly what you want to show up in your search results. Thankfully, editing this isn’t too difficult.

Set up a Custom Function

We’ll start in the functions.php file of your theme. Add this function below.

function excerpt_function($ID, $searchTerms) {
    global $wpdb;
    $thisPost = get_post_meta($ID);
    foreach ($thisPost as $key => $value) {
        if ( false !== stripos($value[0], $searchTerms) ) {
            $found = substr(strip_tags($value[0]), 0, 150);
            echo $found . ' ...';
        }
    }
}

Step 1: (line 1) We’re passing the post ID and the search terms to the function.

Step 2: (line 3) Retrieve the meta data, which is where the custom fields are stored in the database.

Step 3: (lines 4-9) We’ll cycle through the meta array using “foreach” and test if the search terms are in each string.

Step 4: (lines 5-7) If the search term was found in this meta string, we’ll strip the HTML tags and echo it.

Customize the Search Page

The final step is adjusting our search results so we’re showing this excerpt only when a regular excerpt isn’t available. Within the loop on your search page, wherever you’re currently showing the_excerpt(), replace that section with this:

if (has_excerpt() ){
    the_excerpt();
} else {
    excerpt_function($post->ID, $_GET['s']);
}

If the page/post already has a ready-made excerpt, we don’t want to disrupt Relevennsi’s beautiful work. We only want to run this little function if there wouldn’t be an excerpt.

In line 4, we’re also passing the posts’s ID and the search term(s) into the function we just added above.

Summary

This quick fix, just a function in your functions.php and a small change in the PHP for your search results page, can provide the excerpt text for search results that would otherwise have empty excerpts.

However, there are a few downsides. For each search result that we run this function on (those pages/posts composed entirely of ACFs), it will be making another call to the database to get that post’s meta. To mitigate the small negative effect on speed, you can shorten the number of pages you’re showing on your search results page to under 50 and use pagination.

The second downside is that the length of the text in a meta field may not be the full length of your other excerpts. But if the choice is between showing a short amount of text or none for the excerpt, I’d choose showing a short amount.

You can also check out the code on Github:

Join the Discussion

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Comments