-
-
Save searchwpgists/122f5a049e55c9366966f9f49dd633be to your computer and use it in GitHub Desktop.
Adding extra data to indexed entries in SearchWP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!'; | |
// Store custom Custom Field along existing postmeta. | |
$data['meta'][ $my_extra_meta_key ] = $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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment