This Documentation is for SearchWP Version 3

Available since: 1.6.8


Note: Use of this hook will require a manual reindex

Note: This is for SearchWP version 3
Version 4: Adding extra data to indexed entries

SearchWP automatically makes available your Custom Fields when performing searches. There are times though where you may want to include additional, arbitrary data with each post (e.g. Author information) that you’d like to have indexed and searchable as well. This filter facilitates exactly that.

The return data should be an array. The array keys will be used as the meta_key so you must ensure there are no collisions with other post meta. Take note of the array keys as they must be given a search weight on the main SearchWP settings screen after using this filter.

Example: If you want to include author information with each post, begin by adding the following to your active theme’s functions.php:

function my_searchwp_extra_metadata( $extra_meta, $post_being_indexed ) {
// available author meta:
// retrieve the author's name(s)
$author_nicename = get_the_author_meta( 'user_nicename', $post_being_indexed->post_author );
$author_display_name = get_the_author_meta( 'display_name', $post_being_indexed->post_author );
$author_nickname = get_the_author_meta( 'nickname', $post_being_indexed->post_author );
$author_first_name = get_the_author_meta( 'first_name', $post_being_indexed->post_author );
$author_last_name = get_the_author_meta( 'last_name', $post_being_indexed->post_author );
// grab the author bio
$author_bio = get_the_author_meta( 'description', $post_being_indexed->post_author );
// index the author name and bio with each post
$extra_meta['my_author_meta_nicename'] = $author_nicename;
$extra_meta['my_author_meta_display_name'] = $author_display_name;
$extra_meta['my_author_meta_nickname'] = $author_nickname;
$extra_meta['my_author_meta_first_name'] = $author_first_name;
$extra_meta['my_author_meta_last_name'] = $author_last_name;
$extra_meta['my_author_meta_bio'] = $author_bio;
return $extra_meta;
add_filter( 'searchwp_extra_metadata', 'my_searchwp_extra_metadata', 10, 2 );
view raw gistfile1.php hosted with ❤ by GitHub

The author metadata in the above snippet will be included in the index using the outlined array keys as meta keys. You will need to add these meta keys to the SearchWP search engine settings so they are considered when searching.

function my_searchwp_author_meta_keys( $keys )
// the keys we used to store author meta (see for more info)
$my_custom_author_meta_keys = array(
// merge my custom meta keys with the existing keys
$keys = array_merge( $keys, $my_custom_author_meta_keys );
// make sure there aren't any duplicates
$keys = array_unique( $keys );
return $keys;
add_filter( 'searchwp_custom_field_keys', 'my_searchwp_author_meta_keys', 10, 1 );
view raw gistfile1.php hosted with ❤ by GitHub

Once both snippets have been added to your active theme’s functions.php: you will see that your custom author meta keys are included in the SearchWP Custom Fields dropdown, allowing you to give weight to post author metadata. Alternatively you can simply use ‘Any’ custom field to apply the same weight to any post meta search matches.

[wpforms id="3080"]