DescriptionDescription
This filter is called before a facet object (post, term or user) is indexed.
It allows to bypass the default index system by returning a custom array holding at least one row (to insert in the index table) with a facet_value
and facet_name
. If an empty value is set to the facet_value
, row will not be indexed.
facet_value
is used to match posts/terms/users when filtering.facet_name
is displayed as a label in a facet.
ArgumentsArguments
Argument | Type | Description |
$rows | array | Holds index rows of the current object id (empty by default) |
$object_id | integer | Post, term or user id to index |
$facet | array | Holds facet settings |
ExampleExample
PHP
functions.php
function prefix_index_object( $rows, $object_id, $facet ) {
// We skip facet IDs different from 1234.
if ( 1234 !== $facet['id'] ) {
return $rows;
}
$field_1 = get_post_meta( $object_id, 'field_1', true );
$field_2 = get_post_meta( $object_id, 'field_2', true );
// We index several rows/values for a post, term, or user.
// This allows us to combine several sources for one facet.
return [
[
'facet_value' => $field_1,
'facet_name' => $field_1,
],
[
'facet_value' => $field_2,
'facet_name' => $field_2,
],
];
}
add_filter( 'wp_grid_builder/indexer/index_object', 'prefix_index_object', 10, 3 );