Hide from search results

Hi,

On this site - https://www.jonathan-sainsbury.com/?s=M101

If you use the search function you can search by SKU (which has been changed to CODE). Previously you gave me a bit of code which hid some code which was showing in the results, which worked fine. I have recently made changes to the site and have added a PDF product sheet download ability, but that is now showing at the bottom of the search results code.

Can you let me know how to hide this from being returned?

I have one other unrelated question. I see that you now support Woocommerce 3.7.1, how do Install that when the latest update version is 3.8?

Kind regards,

Mark

Hey Mark,

Out of the box, WordPress does not really allow the display of shortcodes inside excerpts. You need to write a custom function and filter out the shortcodes and strings. I’ll provide a sample below.

But, please note that writing custom functions or code, in general, is beyond the scope of our product support. If you will experience issues with the code sample below now or in the future, you will need to either learn WordPress development or hire a WordPress developer to fix and/or enhance it.

Here’s the sample code which I just tested and works on my site.

// Remove Shortcodes from excerpt
function remove_shortcode_from_excerpt( $excerpt ) {

  if ( is_search() ) {
    $excerpt = strip_shortcodes( $excerpt );
  }

  return $excerpt;
}

 add_filter('the_excerpt', 'remove_shortcode_from_excerpt');

// Remove words from excerpt
function remove_words_from_excerpt( $excerpt ) {
  
 // Words are purposely broken down so you'll see how to add more strings or text.
  $words = array( 
    'Download Product',
    'Information',
    'Sheet [PDF]',
  );
  
  foreach ( $words as $word ) {
      $excerpt = str_replace( $word, '', $excerpt);
  }
  return $excerpt;
}

 add_filter('get_the_excerpt', 'remove_words_from_excerpt');

###Result screenshots:

Before:

After:

===============================================================================

Regarding your other question, you can download previous versions of plugins in WordPress repository in the plugin’s Advanced page like this https://wordpress.org/plugins/woocommerce/advanced/. Scroll down the Previous Versions section, download the needed version and then install it to your site by uploading it.

Hope that helps and thank you for understanding.

Hi Christian,

Thank you very much for that bit of code. I am guessing that I add this into the functions.php of the child theme?

Kind regards,

Mark

Hi Mark,

Yes, that it right.

Thanks a lot that has solved my issue :smiley:

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.