/indexer/index_object

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

ArgumentTypeDescription
$rowsarrayHolds index rows of the current object id (empty by default)
$object_idintegerPost, term or user id to index
$facetarrayHolds facet settings

ExampleExample

PHP
functions.php
function prefix_index_object( $rows, $object_id, $facet ) {

	// We skip facet IDs different from 1.
	if ( 1 !== $facet['id'] ) {
		return $rows;
	}

	$start_date = get_post_meta( $object_id, 'start_date', true );

	if ( empty( $start_date ) ) {
		return $rows;
	}

	// It may contains several rows (nested arrays).
	// Return rows to insert in the index table.
	return [
		[
			'facet_value' => date( 'Y-m-d', strtotime( $start_date ) ),
			'facet_name'  => 'Start date',
		],
	];

}

add_filter( 'wp_grid_builder/indexer/index_object', 'prefix_index_object', 10, 3 );