Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created June 6, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/5479fabb5ea06263b015a1d7614a2396 to your computer and use it in GitHub Desktop.
Save jchristopher/5479fabb5ea06263b015a1d7614a2396 to your computer and use it in GitHub Desktop.
Use SearchWP Term Highlight to automatically populate an excerpt for Media
<?php
function searchwp_term_highlight_auto_excerpt( $excerpt ) {
global $post;
if ( ! function_exists( 'searchwp_term_highlight_get_the_excerpt_global' ) || ! is_search() || 'attachment' !== get_post_type( $post ) ) {
return $excerpt;
}
// prevent recursion
remove_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
$global_excerpt = searchwp_term_highlight_get_the_excerpt_global( $post->ID, null, get_search_query() );
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
return $global_excerpt;
}
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment