Note: You are viewing the documentation for Version 4View Version 3 Docs

Adding extra data to indexed entries

Many times the content to be indexed is visible/editable on the edit screen for the entries you’re working with, but there are also times where you want to append extra data to entries as SearchWP indexes them so as to make that data searchable as well.

We can use a couple of SearchWP’s hooks to include arbitrary data alongside your entry content when SearchWP’s indexer is running.

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// @link https://searchwp.com/documentation/knowledge-base/adding-extra-data-to-indexed-entries/
// Adding extra data to indexed entries in SearchWP.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
$my_extra_meta_key = 'my_extra_meta';
// SearchWP can index any data type, array/object values will all be processed.
// For this example we will use a string:
$my_extra_meta = 'This content will be searchable!';
// Entry data must be tokenized.
$data['meta'][ $my_extra_meta_key ] = \SearchWP\Utils::tokenize( $my_extra_meta );
return $data;
}, 20, 2 );
// Add 'extra' meta as available option for your Source Attributes.
add_filter( 'searchwp\source\attribute\options', function( $keys, $args ) {
if ( $args['attribute'] !== 'meta' ) {
return $keys;
}
// This key is the same as the one used in the searchwp\entry\data hook above, they must be the same.
$my_extra_meta_key = 'my_extra_meta';
// Add "Extra Meta" Option if it does not exist already.
if ( ! in_array(
$my_extra_meta_key,
array_map( function( $option ) { return $option->get_value(); }, $keys )
) ) {
$keys[] = new \SearchWP\Option( $my_extra_meta_key, 'Extra Meta' );
}
return $keys;
}, 20, 2 );

With those hooks added to your SearchWP Customizations plugin, you’ll see the ‘Extra Meta’ Custom Field show up when you search for extra as it matches the custom Option name we created in the snippet above.

Screenshot of Extra Meta Custom Field

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP