Skip to main contentSkip to footer

If your posts have lots of programming code examples in <pre> and <code> tags, those might look pretty bad in the search results. A snippet of programming code isn’t usually a good excerpt, and if you use those tags purely for code snippets, they won’t likely contain significant search content, either.

Fortunately it’s easy to completely remove all <pre> and <code> tags and their content both in indexing and excerpt-building. All you need to do is to add the following code to your theme functions.php:

add_filter( 'relevanssi_post_content', 'rlv_pre_code', 10 );
add_filter( 'relevanssi_excerpt_content', 'rlv_pre_code', 10 );
function rlv_pre_code( $content ) {
	$content = preg_replace( '#<pre.*?</pre>#mis', '', $content );
	$content = preg_replace( '#<code.*?</code>#mis', '', $content );
	return $content;
}

This will find all <pre> and <code> tags in posts and replace them and their contents with nothing, both when indexing and when building excerpts. This will immediately take effect for excerpts, and requires reindexing to apply in indexing.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

MemberPress Downloads add-on
The Downloads add-on for MemberPress adds downloadable files to MemberPress. These files are stored outside the Media Library, so by…
WP Event Manager

Using Relevanssi with WP Event Manager requires you to adjust the search process in WP Event Manager a bit. Fortunately, the plugin has good filters you can use. Add this to your site: add_filter( ‘get_event_listings_query_args’, function( $args ) { $args[‘relevanssi’] = true; return $args; } ); This will adjust the……event listing query arguments to switch on the Relevanssi flag, causing Relevanssi to process the results. This way you’ll get the Relevanssi results in the WP Event Manager event listing search. These Relevanssi results will also be cached by the WP Event Manager query caching. Thanks to dustyyy on WP…

Indexing private custom fields for admins only

…); function rlv_private_custom_fields( $match ) { global $wp_query; $admin_search = $wp_query->is_admin; if ( ! $admin_search && empty( $wp_query->query_vars ) ) { $admin_search = true; } if ( ! $admin_search ) { $customfield_detail = json_decode( $match->customfield_detail ); if ( isset( $customfield_detail->private_custom_field ) ) { $match->weight = 0; } if ( isset(…I use private custom fields. I want to index them in the administration but not on the public site. How to do ? Initially this seems impossible to do with Relevanssi, but it’s possible. Doing this requires Relevanssi Premium. The free version doesn’t know which custom field matches the search term,……$customfield_detail->another_private_field ) ) { $match->weight = 0; } } return $match; } Now when a search is made on the front end and the custom field that has the hit matches one of the private fields, the post weight is set to zero. For  more sophistication, the code should probably…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed