Prevent Indexing Segments of Content
Note: Unless supported by additional Sources, this article applies only to SearchWP’s core \WP_Post
-based Sources (e.g. Posts, Pages, Custom Post Types)
Using SearchWP’s Shortcode-parsing ability you can control what content is not indexed/searchable.
Begin by ensuring that the checkbox to enable Shortcode processing is ticked on the Advanced tab of the SearchWP settings screen.
Next we will need to implement a custom Shortcode. For this article we’ll be wrapping the content we want to exclude in [searchwp_no_index]
[/searchwp_no_index]
tags:
Any content included within our custom Shortcode tags will be excluded when indexing (and therefore from searching).
All hooks should be added to your custom SearchWP Customizations Plugin.
<?php | |
// Add a Shortcode to exclude content from SearchWP's index. | |
// @link https://searchwp.com/documentation/knowledge-base/prevent-indexing-segments-of-content/ | |
add_shortcode( 'searchwp_no_index', function( $atts, $content = null ) { | |
// If the indexer is running do not return anything, | |
// else return the content contained in the Shortcode. | |
return did_action( 'searchwp\indexer\batch' ) ? '' : $content; | |
} ); |
Note: make sure this hook is in place before adding your Shortcodes, when you add the Shortcodes to the content and click Update the post will be automatically purged and reindexed by SearchWP.