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( $taxonomy_names, $post ) {
if ( is_object( $post ) && 'event' === $post->post_type ) {
$taxonomy_names[] = 'event-format';
$taxonomy_names[] = 'event-city';
}
return $taxonomy_names;
}
Note the convention above builds a list that looks as follows; it’s an indexed array.
[
'event-format',
'event-city',
]
Excluding post taxonomies from an allow list
If there are certain taxonomies that should not be indexed, you can use the same filter as above (using category
and post_tag
as an example):
function mysite_indexable_post_tax( $taxonomy_names, $post ) {
$exclude_list = array( 'category', 'post_tag' );
// 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 );