0

I'm working on an older site with a million plugins that just can't handle SearchWP and trying to mimic some of that plugins functionality.

Things that ARE working:

  1. my search includes an ACF textarea field with a big chunk of content client's drop in it (a transcript)
  2. I'm able to create an excerpt from that ACF textarea field for search results
  3. I'm able to highlight the keyword in the search results

What's NOT working:

I'd like a way to output the part of the full contents of ACF textarea field that contains the keyword. Right now the excerpt is being created with wp_trim_wordsbut always starts at the beginning. The highlighted keyword is snagged with the following code I found in another similar question on here:

$raw_content = get_field('transcript');
$trimmed_content = wp_trim_words($raw_content);
$keys = implode('|', explode(' ', get_search_query()));
$raw_content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $raw_content);

I don't have a preference for php or js.

1
  • 1
    if you replace $raw_content = get_field('transcript'); with $raw_content = .....;, add in a $search_term variable, and remove all references to ACF, you can both bring this question on-topic here, then make it offtopic again by turning it into a generic PHP question that can be asked on stackoverflow, and answered by 10x as many developers. You're heavily limiting your options by constricting your search to ACF, and more so by only looking for WordPress based solutions. Where the $raw_content gets its value from is irrelevant to your problem, and ACF is just there to confuse people
    – Tom J Nowell
    Mar 10 at 15:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.