---
name: kimaai_embedding_chunk_text
type: filter
since: 1.4.2
tier: premium
feature: embeddings
area: embedding-search
summary: Changes the text a chunk is embedded with, for example to prefix it with a product's brand, without changing the text stored and quoted.
parameters:
  - name: text
    type: string
    description: The chunk's text; it starts with a "Context:" line naming its heading or the post's title.
  - name: post
    type: WP_Post
    description: The post the chunk belongs to.
  - name: chunk
    type: array
    description: The chunk, with text, chunk_index, heading_path, anchor, token_count, start_char and end_char.
  - name: clean
    type: array
    description: The post's prepared text, with lang, text, title and html.
returns:
  type: string
  description: The text to embed.
---

# kimaai_embedding_chunk_text

## When it fires

In every sync of the knowledge base, for each chunk of each post, twice: first to tell whether the chunk changed since the last sync (a hash of the returned text and the chunk's index data), then, when it did or the sync is forced, right before the embedding is requested from the AI provider. Return the same text for the same input, or every sync embeds every chunk again.

The returned text decides what the vector means, so what the knowledge search finds. It is not stored: the snippet shown under answers and the text the AI reads keep the chunk's own text. To add text that is also shown and read, use `kimaai_embedding_extra_text`.

When the callback starts returning another text, the changed chunks are embedded again on their next sync.

## Example

```php
add_filter(
  'kimaai_embedding_chunk_text',
  function (string $text, WP_Post $post): string {
    if ($post->post_type !== 'product') {
      return $text;
    }

    // Questions name the brand, the product texts rarely do: each chunk of a product is found by its brand too.
    $brand = (string) get_post_meta($post->ID, 'brand', true);

    return $brand !== '' ? 'Brand: ' . $brand . "\n" . $text : $text;
  },
  10,
  2
);
```

## Related

- `kimaai_embedding_extra_text`: adds text to the post, stored and shown with its chunks.
- `kimaai_embedding_vector_metadata`: stores data with each chunk.
