wpfts_chunks_before_indexing (Filter)
The wpfts_chunks_before_indexing
filter in WP Fast Total Search is called right before post data is indexed. It receives the $chunks
array, containing data prepared for indexing, and allows you to make final changes to this data before it’s added to the index.
When to Use
This filter can be used for:
- Final data processing before indexing: For example, you can remove HTML tags, convert special characters, or convert text to lowercase.
- Adding data from other sources: You can add data from related taxonomies, meta fields, or other posts to
$chunks
. - Removing data from the index: You can remove specific clusters or parts of them from the
$chunks
array to exclude them from the index. - Debugging the indexing process: You can use this filter to output the contents of
$chunks
and check what data will be indexed.
Arguments
$chunks
(array): An array of data prepared for indexing. Array keys are cluster names, values are strings or arrays of strings with data.$post
(WP_Post object): The WordPress post object.
Return Value
$chunks
(array): The modified array of data for indexing.
Example (Removing HTML tags from all clusters)
add_filter('wpfts_chunks_before_indexing', 'strip_tags_from_chunks', 10, 2);
function strip_tags_from_chunks($chunks, $post) {
foreach ($chunks as $cluster => &$data) { // & - important! Modifying data by reference
if (is_string($data)) {
$data = wp_strip_all_tags($data);
} elseif (is_array($data)) {
// handling the case where data in the cluster is an array of strings
foreach ($data as &$item) {
$item = wp_strip_all_tags($item);
}
}
}
return $chunks;
}
Example (Adding taxonomy terms to the index)
add_filter('wpfts_chunks_before_indexing', 'add_taxonomy_terms_to_index', 10, 2);
function add_taxonomy_terms_to_index( $chunks, $post ) {
$taxonomies = array('category', 'post_tag'); // list of taxonomies
$terms_string = '';
foreach($taxonomies as $taxonomy) {
$terms = get_the_terms( $post->ID, $taxonomy );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$terms_string .= $term->name . ' ';
}
}
}
if ($terms_string) {
$chunks['taxonomy_terms'] = $terms_string;
}
return $chunks;
}
Important Notes
- The
wpfts_chunks_before_indexing
filter is called immediately before indexing, so this is the last chance to modify the data that will be added to the index. - Changes made to the
$chunks
array inside the handler of this filter will be saved in the index.
This filter provides developers with maximum flexibility in managing the data indexed by the WP Fast Total Search plugin.