Skip to content

Indexing user meta

By default, the users indexable feature is not activated. Once the users indexable feature is activated, all non-protected user meta is indexed.

The non-protected user meta that is indexed can be customized with the ep_prepare_user_meta_data filter. In this code example, indexing is limited to only the user meta fields nickname, first_name, last_name and description:

add_filter( 'ep_prepare_user_meta_data', 'filter_prepared_user_meta', 10, 2 );
function filter_prepared_user_meta( $meta, $user_id ) {
    $meta_to_index = [ 'nickname', 'first_name', 'last_name', 'description' ];
    foreach( $meta as $key => $value ) {
        if ( ! in_array( $key, $meta_to_index ) ) {
            unset( $meta[ $key ] );
        }
    }
    return $meta;
}

When indexing user meta for sites that have a large number of users, or large amounts of user meta, this error may be returned due to Elasticsearch’s limitations on fields: [illegal_argument_exception] Limit of total fields [5000] has been exceeded. The error can be prevented by indexing fewer fields.

Last updated: December 22, 2023

Relevant to

  • WordPress