wpfts_index_post (Filter)
The wpfts_index_post
filter provides developers with a powerful tool to modify the data indexed by the WP Fast Total Search
plugin. It’s called after the plugin has extracted data from the post (title, content, excerpt, and other fields defined by indexing rules), but before this data is actually added to the search index. This allows you to add, modify, or remove data before indexing.
When to Use
This hook is ideal for the following scenarios:
- Adding data from custom fields to the index.
- Modifying content before indexing (e.g., removing HTML tags, formatting text).
- Adding data from related taxonomies or other posts.
- Excluding specific parts of content from the index.
Arguments
$chunks
(array): An array of data prepared for indexing. The array keys represent cluster names (e.g.,post_title
,post_content
,post_excerpt
, and any other clusters defined by indexing rules). The array values are strings or arrays of strings containing data for indexing. Important: This argument is passed by reference (&), so any changes made to the$chunks
array inside the handler function will be reflected in the data that will be indexed.$post
(WP_Post
object): A WordPress object representing the current post being indexed. This provides access to all post properties, such as ID, title, publication date, etc. You can use this object to retrieve additional data related to the post and add it to$chunks
.$is_refresh_raw_cache
(bool): A flag indicating whether the post data cache was forcibly refreshed.true
if the cache was refreshed,false
otherwise. This parameter is rarely used by addon developers.
Return Value
$chunks
(array): The modified array of data for indexing. The handler function must return the$chunks
array, even if no changes were made to it.
Example
/**
* Adds the value of the "my_custom_field" custom field to the index.
*/
add_filter('wpfts_index_post', 'my_wpfts_index_post_handler', 10, 3);
function my_wpfts_index_post_handler($chunks, $post, $is_refresh_raw_cache) {
// Get the custom field value.
$custom_field_value = get_post_meta($post->ID, 'my_custom_field', true);
// Add the value to the new "my_custom_cluster" cluster.
if ($custom_field_value) {
if (!is_array($custom_field_value)) {
$custom_field_value = array($custom_field_value);
}
$chunks['my_custom_cluster'] = $custom_field_value;
}
// Return the modified array.
return $chunks;
}
Example 2 (working with arrays)
add_filter('wpfts_index_post', 'add_multiple_meta_fields', 10, 3);
function add_multiple_meta_fields($chunks, $post, $is_refresh_raw_cache)
{
$meta_fields = array('meta_field_1', 'meta_field_2', 'meta_field_3');
$meta_values = array();
foreach ($meta_fields as $field) {
$value = get_post_meta($post->ID, $field, true);
if ($value) {
$meta_values[$field] = is_array($value) ? implode(' ', $value) : $value; // Joins array values into a string
}
}
if (count($meta_values) > 0) {
if (isset($chunks['additional_meta'])) {
$chunks['additional_meta'] = array_merge($chunks['additional_meta'], $meta_values);
} else {
$chunks['additional_meta'] = $meta_values;
}
}
return $chunks;
}
Important Notes
- Changes made to
$chunks
will affect search results. Make sure you only add relevant data. - If you add data to a new cluster, make sure you also configure the weight for that cluster in the plugin settings or via the
wpfts_cluster_weights
filter for it to be considered when calculating relevance.
This hook is one of the most effective ways to extend the functionality of WP Fast Total Search
. Use it wisely, and you can customize the search on your site as accurately as possible.