Indexing post types
Enterprise Search automatically indexes all public post types.
In this example, a custom function uses pre_get_posts
to customize search for an event
custom post type. Because the event
custom post type is public, it is in the post_type
allow list and indexed by default.
add_action( 'pre_get_posts', 'my_event_query_filter' );
function my_event_query_filter( $query ) {
if ( $query->is_main_query() && get_query_var( 'events', false ) ) {
$query->set( 'post_type' ), 'event' );
}
}
Note
Indexing can be enabled for all post types, including private post types, by activating the Protected Content feature, but also risks exposing non-public content to unauthorized users.
Adding post types to the allow list
Use the ep_indexable_post_types
filter to customize the indexable post types. This filter is useful if there is a need for specific private post types to be indexed, or to exclude specific public post types from indexing.
In this code example, the private post type attachment
is added to the allow list for indexing:
function mysite_indexable_post_types( $types ) {
$types['attachment'] = 'attachment';
return $types;
}
add_filter( 'ep_indexable_post_types', 'mysite_indexable_post_types', 10, 1 );
The ep_indexable_post_types
filter uses a list with the below array format:
[
'event' => 'event',
'post' => 'post',
'page' => 'page',
]
Excluding post types from the allow list
To exclude specific post types (e.g. ones which do not contain information that will be displayed to users), use the same allow list to block indexing by programmatically removing the entry.
In this code example, the ai_log
, msm_sitemap
, wlo_option
, vip-legacy-redirect
custom post types are excluded from indexing:
function mysite_events_indexable_post_types( $types ) {
$deny_list = array( 'ai_log', 'msm_sitemap', 'wlo_option', 'vip-legacy-redirect' );
// unset the items with values matching the deny list
$types = array_diff( $types, $deny_list );
return $types;
}
add_filter( 'ep_indexable_post_types', 'mysite_events_indexable_post_types', 10, 1 );
Last updated: December 22, 2023