Skip to content

Enable Enterprise Search for the WordPress Admin

By default, Enterprise Search is not enabled for use in the WordPress Admin. Activating the Protected Content feature will send WordPress Admin search queries to Elasticsearch, but also risks exposing non-public content to unauthorized users. The ep_admin_wp_query_integration and ep_ajax_wp_query_integration filters can be used to more selectively enable Enterprise Search for the WordPress Admin.

Selectively offload WordPress Admin query

To selectively offload a WordPress Admin query, use the ep_admin_wp_query_integration filter in combination with the allow lists for post statuses and post types (i.e. private post types). In this code example, Enterprise Search is enabled for querying a private Custom Post Type (CPT) named biography with the post statuses of draft, future, pending and private:

// Only offload the admin posts query in the edit page for 'biography' posts
add_filter( 'ep_admin_wp_query_integration', 'vip_admin_es_admin_wp_query_integration', 9999 );
function vip_admin_es_admin_wp_query_integration( $query ) {
    if ( is_admin() ) {
        $current_screen = get_current_screen();
        if ( isset( $current_screen->id ) && 'edit-biography' === $current_screen->id ) {
            return true;
        }
    }
	return false;
}

// Add private post statuses for posts to be indexed
function vip_indexable_admin_post_statuses( $status ) {
    $status = array_merge( $status, [ 'draft', 'future', 'pending', 'private' ] );
    return $status;
}
add_filter( 'ep_indexable_post_status', 'vip_indexable_admin_post_statuses' );

// Add CPT 'biography'
function mysite_indexable_post_types( $types ) {
	$types['biography'] = 'biography';
	return $types;
}
add_filter( 'ep_indexable_post_types', 'mysite_indexable_post_types', 10, 1 );

Selectively offload WordPress Admin queries when Protected Content is activated

If the Protected Content feature is activated, all admin queries are offloaded (including post types that are not indexed). The ep_admin_wp_query_integration filter can be used when Protected Content is active to prevent specific post type admin queries from being offloaded.

// Do not offload the admin query in the edit page for 'event' or 'wp_block' post types
add_filter( 'ep_admin_wp_query_integration', 'vip_admin_es_admin_wp_query_integration', 99999 );
function vip_admin_es_admin_wp_query_integration( $query ) {
    $unindexed_post_types = [ 'event', 'wp_block' ];
    if ( is_admin() ) {
        $current_screen = get_current_screen();
        foreach( $unindexed_post_types as $unindexed_post_type ) {
           $screen = 'edit-' . $unindexed_post_type;
           if ( isset( $current_screen->id ) && $screen === $current_screen->id ) {
               return false;
           }
        }
    }
    return true;
}

Using ep_ajax_wp_query_integration

Because the Media Library’s search is powered by AJAX, enabling Enterprise Search to perform Media Library searches requires the ep_ajax_wp_query_integration filter:

add_filter( 'ep_ajax_wp_query_integration', 'vip_es_enable_ajax_admin' );
function vip_es_enable_ajax_admin( $enable ) {
    if ( is_admin() ) {
        $enable = true;
    }
    return $enable;
}

If the Protected Content feature is not activated, add attachment to the allow list for post types and inherit to the allow list for post statuses and re-index.

// Enable indexing of 'attachment' post types
function mysite_indexable_post_types( $types ) {
	$types['attachment'] = 'attachment';
	return $types;
}
add_filter( 'ep_indexable_post_types', 'mysite_indexable_post_types', 10, 1 );

// Enable indexing of 'inherit' post statuses
function mysite_indexable_post_statuses( $status ) {
    $status = array_merge( $status, [ 'inherit' ] );
    return $status;
}
add_filter( 'ep_indexable_post_status', 'mysite_indexable_post_statuses', 10, 1 );

Last updated: February 29, 2024

Relevant to

  • WordPress