Skip to content

Indexing post taxonomies

Similar to post types, any registered taxonomy that is public will be indexed by default.

E.g. If the taxonomy was registered with register_taxonomy and the public or publicly_queryable attributes were left to the default of true, or explicitly set to true, they will already be in the allow list. Otherwise, they will need to be added. Alternatively, 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 or excluding post taxonomies to an allow list

If you want to customize the post taxonomies being indexed, use the vip_search_post_taxonomies_allow_list filter. 

For example, if we want to index the event-format and event-city taxonomies only for the event post_type:

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

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

	return $taxonomy_names;
}

The convention above builds an indexed array.

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

Alternatively, if there are certain taxonomies that should not be indexed, you can use the same filter as above:

function mysite_indexable_post_tax( $taxonomy_names, $post ) {
	$exclude_list = array( 'event-format', 'event-city' );
	// unset the items with values matching the deny list
	$types = array_diff( $taxonomy_names, $exclude_list );

	return $types;
}
add_filter( 'vip_search_post_taxonomies_allow_list', 'mysite_indexable_post_tax', 10, 2 );

Taxonomies must be indexed to be used in queries

For example, our custom post type of event has at 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
	)
),

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

Last updated: December 22, 2023

Relevant to

  • WordPress