Skip to content

Backgrounds

How-to Guides

Technical References

Enterprise Search /

Identify which post taxonomies are indexed

This is the second step in the audit we discussed in the introduction: post taxonomies.

Taxonomies must be indexed to be used in queries

Our example custom post type of event has at least two terms for the event-format taxonomy: virtual, and in_person

We have a search that includes a taxonomy query in the arguments:

	'tax_query' => array(
		array(
			'taxonomy' => 'event-format',
			'field'    => 'slug',
			'terms'    => $format
			)
		),

It’s the $format variable (coming from the search form or a query parameter) that indicates whether the user is looking for virtual, in_person, possibly both (in an array), or a future format that hasn’t been implemented yet.

Public taxonomies are indexed by default

Just like post types, any registered taxonomy that is public will be indexed by default.

If the taxonomy was registered with register_taxonomy and the public or publicly_queryable attributes were left to default true, or explicitly set to true, they should already be in the allow list. If not, they will need to be added. And if they are public but you’d prefer to not allow searches on them, you can disallow them using the same technique as post types.

Adding post taxonomies to an allow list

So the actual taxonomy we need to index here is event-format – I’ve also included event-city here as an additional example; often multiple taxonomies will be declared in the same filter.

These taxonomies are only being indexed when the custom post type is event.

add_filter( 'vip_search_post_taxonomies_allow_list', 'mysite_events_indexable_post_tax', 10, 2 );

function mysite_events_indexable_post_tax( $allow, $post ) {
	if ( is_object( $post ) && 'event' === $post->post_type ) {
		$allow[] = 'event-format';
		$allow[] = 'event-city';
	}

	return $allow;
}

Note the convention above builds a list that looks as follows; it’s an indexed array.

[
	'event-format',
	'event-city',
]

Last updated: June 24, 2021