Keyword search is fine until it isn’t. The moment a reader asks a question in natural language, LIKE '%term%' falls over. Retrieval-augmented generation (RAG) fixes that by turning your posts into vectors and matching meaning rather than characters. This tutorial builds a production-shaped RAG search endpoint on WordPress: chunking on save_post, batched embedding calls, a dedicated {$wpdb->prefix}post_embeddings table, a WP-CLI backfill command, and a capability-checked /wp-json/ai/v1/search route that scores results with cosine similarity in plain PHP.
What you need before you start
- WordPress 6.4 or later, PHP 8.1+, and WP-CLI installed. The code below was run on WordPress 7.1.3 with PHP 8.2.
- An OpenAI API key (or a Gemini key: the shape is nearly identical). Store it in
wp-config.phpasWPTH_OPENAI_KEY, never in the database. - A child theme or small plugin to hold the code. A plugin is safer: the logic survives theme switches.
- Comfort with
$wpdb,register_rest_route, and the save_post hook.
A custom table, because post meta is key-value and indexed for lookups, not for scanning thousands of 1536-dimension float arrays. A dedicated table lets you add a
post_idindex, keepchunk_textalongside the vector, and delete rows in bulk. See the $wpdb reference for the API you will use throughout.
Step 1 Create the embeddings table
Run this on plugin activation. dbDelta is idempotent, so re-running it after a schema change is safe. The content_hash column is the key to skipping unchanged posts later.
<?php
function wpth_rag_install_table() {
global $wpdb;
$table = $wpdb->prefix . 'post_embeddings';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table} (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT UNSIGNED NOT NULL,
chunk_index SMALLINT UNSIGNED NOT NULL DEFAULT 0,
chunk_text LONGTEXT NOT NULL,
content_hash CHAR(64) NOT NULL,
embedding LONGTEXT NOT NULL,
model VARCHAR(64) NOT NULL DEFAULT '',
updated_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY post_id (post_id),
KEY content_hash (content_hash)
) {$charset};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'wpth_rag_install_table' );
Step 2 Chunk content and hash it
Embedding models have a token ceiling, and long posts dilute their own signal. Split content into overlapping windows of roughly 800 characters with 100 characters of overlap: the overlap preserves context across boundaries so a sentence cut in half still appears whole in one chunk.
The hash is computed from the raw post content, not the chunks. If the hash matches what is already stored, the post is unchanged and you skip the API call entirely. This single check saves the majority of your embedding spend.
<?php
function wpth_rag_chunk_content( string $content, int $size = 800, int $overlap = 100 ): array {
$text = wp_strip_all_tags( $content );
$text = preg_replace( '/\s+/', ' ', trim( $text ) );
$len = mb_strlen( $text );
$chunks = [];
for ( $i = 0; $i < $len; $i += ( $size - $overlap ) ) {
$chunks[] = mb_substr( $text, $i, $size );
if ( $i + $size >= $len ) {
break;
}
}
return $chunks;
}
Step 3 Call the embeddings API in batches
OpenAI’s text-embedding-3-small accepts an array of inputs per request, which is dramatically faster than one call per chunk. Batch in groups of 32: small enough to stay under the payload limit, large enough to amortise latency. Always check the HTTP status before decoding; a 429 or 401 will otherwise produce a confusing json_decode warning.
<?php
function wpth_rag_embed( array $inputs ): array|WP_Error {
$api_key = defined( 'WPTH_OPENAI_KEY' ) ? WPTH_OPENAI_KEY : '';
if ( ! $api_key ) {
return new WP_Error( 'no_key', 'Missing OpenAI API key' );
}
$response = wp_remote_post(
'https://api.openai.com/v1/embeddings',
[
'timeout' => 30,
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( [
'model' => 'text-embedding-3-small',
'input' => $inputs,
] ),
]
);
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'api_error', 'Embeddings API returned ' . wp_remote_retrieve_response_code( $response ) );
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return array_map( fn( $item ) => $item['embedding'], $body['data'] );
}
Step 4 Hook into save_post
Index on publish, not on every keystroke. The save_post hook fires on revisions and autosaves too, so guard against both. Replace the post’s existing rows rather than adding to them, otherwise edits leave orphaned vectors that pollute your search results. Embed every chunk before deleting anything: if a batch fails halfway, the post keeps its old vectors and the next save tries again.
<?php
add_action( 'save_post', 'wpth_rag_index_post', 20, 3 );
function wpth_rag_index_post( $post_id, $post, $update ) {
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
if ( 'publish' !== $post->post_status || ! in_array( $post->post_type, [ 'post', 'page' ], true ) ) {
return;
}
$hash = hash( 'sha256', $post->post_content );
global $wpdb;
$table = $wpdb->prefix . 'post_embeddings';
$existing = $wpdb->get_var( $wpdb->prepare(
"SELECT content_hash FROM {$table} WHERE post_id = %d LIMIT 1",
$post_id
) );
if ( $existing === $hash ) {
return; // unchanged — skip the API call
}
$chunks = wpth_rag_chunk_content( $post->post_content );
if ( empty( $chunks ) ) {
return;
}
// Embed every chunk first. If any batch fails, nothing has changed yet and the next save retries.
$vectors = [];
foreach ( array_chunk( $chunks, 32 ) as $batch ) {
$batch_vectors = wpth_rag_embed( $batch );
if ( is_wp_error( $batch_vectors ) ) {
error_log( 'RAG embed failed: ' . $batch_vectors->get_error_message() );
return;
}
$vectors = array_merge( $vectors, $batch_vectors );
}
$wpdb->delete( $table, [ 'post_id' => $post_id ], [ '%d' ] );
foreach ( $chunks as $i => $chunk ) {
$wpdb->insert( $table, [
'post_id' => $post_id,
'chunk_index' => $i, // position in the whole post, not in one API batch
'chunk_text' => $chunk,
'content_hash' => $hash,
'embedding' => wp_json_encode( $vectors[ $i ] ),
'model' => 'text-embedding-3-small',
'updated_at' => current_time( 'mysql', true ),
] );
}
}
// Drop a post's vectors as soon as it stops being public (draft, private, pending, trash).
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
if ( 'publish' === $old_status && 'publish' !== $new_status ) {
global $wpdb;
$wpdb->delete( $wpdb->prefix . 'post_embeddings', [ 'post_id' => $post->ID ], [ '%d' ] );
}
}, 10, 3 );
An earlier version deleted the old rows first and wrote each batch as it came back. On a post long enough for two batches, that stored the wrong chunk positions, and a failed second batch left the post half-indexed for good, because the stored hash told every later save that nothing had changed:
A 37-chunk post, saved once normally and once with the second API call failing, then re-saved: earlier version (test site: WordPress 7.1.3, PHP 8.2; the embeddings endpoint is stubbed because the test site has no internet, so scores come from shared words rather than meaning):
Long post #8: 37 chunks expected, 37 rows stored, chunk_index values 0..31, 32 distinct
Second batch fails with HTTP 429:
Long post #9: 37 chunks expected, 32 rows stored, chunk_index values 0..31, 32 distinct
After a re-save with the API working: 32 rows stored
The version above (same test site):
Long post #8: 37 chunks expected, 37 rows stored, chunk_index values 0..36, 37 distinct
Second batch fails with HTTP 429:
Long post #9: 37 chunks expected, 0 rows stored, chunk_index values
After a re-save with the API working: 37 rows stored
The transition_post_status hook at the end matters as much: it removes a post’s vectors as soon as it is drafted, made private or trashed.
Step 5 Backfill existing posts with WP-CLI
New posts index themselves, but the archive needs a one-off pass. A WP-CLI command is the right tool: it runs without a timeout, prints a progress bar, and can be re-run safely thanks to the hash check. See the WP-CLI commands reference for the full API.
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'wpt-rag backfill', function() {
$ids = get_posts( [
'post_type' => [ 'post', 'page' ],
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
] );
$bar = \WP_CLI\Utils\make_progress_bar( 'Indexing', count( $ids ) );
foreach ( $ids as $post_id ) {
wpth_rag_index_post( $post_id, get_post( $post_id ), true );
$bar->tick();
}
$bar->finish();
WP_CLI::success( 'Backfill complete.' );
} );
}
Run it with wp wpt-rag backfill. On a 2,000-post site expect roughly 10–20 minutes depending on chunk count and API latency.
Step 6 Expose the REST endpoint
The route lives under a namespaced path so it cannot collide with core. The permission_callback is mandatory in modern WordPress: returning current_user_can( 'read' ) restricts access to logged-in users. If you want anonymous search, return true but add a rate limit; otherwise you are paying for every bot that finds the URL. See Adding custom endpoints.
Cosine similarity is the standard metric for normalised embeddings: the dot product of two vectors divided by the product of their magnitudes. It runs comfortably in PHP for a few thousand rows; beyond that, move scoring to a vector database.
<?php
add_action( 'rest_api_init', function() {
register_rest_route( 'ai/v1', '/search', [
'methods' => 'GET',
'permission_callback' => fn() => current_user_can( 'read' ),
'args' => [
'q' => [
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => fn( $v ) => is_string( $v ) && mb_strlen( $v ) >= 3,
],
'limit' => [ 'default' => 5, 'sanitize_callback' => 'absint' ],
],
'callback' => 'wpth_rag_search_handler',
] );
} );
function wpth_rag_cosine( array $a, array $b ): float {
$dot = $na = $nb = 0.0;
$n = min( count( $a ), count( $b ) );
for ( $i = 0; $i < $n; $i++ ) {
$dot += $a[ $i ] * $b[ $i ];
$na += $a[ $i ] * $a[ $i ];
$nb += $b[ $i ] * $b[ $i ];
}
return ( $na && $nb ) ? $dot / ( sqrt( $na ) * sqrt( $nb ) ) : 0.0;
}
function wpth_rag_search_handler( WP_REST_Request $request ) {
$query = $request->get_param( 'q' );
$limit = min( 20, (int) $request->get_param( 'limit' ) );
$vector = wpth_rag_embed( [ $query ] );
if ( is_wp_error( $vector ) ) {
return new WP_Error( 'embed_failed', $vector->get_error_message(), [ 'status' => 502 ] );
}
$q_vec = $vector[0];
global $wpdb;
$table = $wpdb->prefix . 'post_embeddings';
// Only score chunks from posts that are still published.
$rows = $wpdb->get_results(
"SELECT e.post_id, e.chunk_text, e.embedding
FROM {$table} e
INNER JOIN {$wpdb->posts} p ON p.ID = e.post_id
WHERE p.post_status = 'publish'",
ARRAY_A
);
$scored = [];
foreach ( $rows as $row ) {
$vec = json_decode( $row['embedding'], true );
if ( ! is_array( $vec ) ) {
continue;
}
$score = wpth_rag_cosine( $q_vec, $vec );
$pid = (int) $row['post_id'];
if ( ! isset( $scored[ $pid ] ) || $score > $scored[ $pid ]['score'] ) {
$scored[ $pid ] = [
'score' => $score,
'excerpt' => wp_trim_words( $row['chunk_text'], 40 ),
];
}
}
uasort( $scored, fn( $a, $b ) => $b['score'] <=> $a['score'] );
$top = array_slice( $scored, 0, $limit, true );
$results = [];
foreach ( $top as $pid => $data ) {
$results[] = [
'post_id' => $pid,
'title' => get_the_title( $pid ),
'url' => get_permalink( $pid ),
'score' => round( $data['score'], 4 ),
'excerpt' => $data['excerpt'],
];
}
return rest_ensure_response( [ 'query' => $query, 'results' => $results ] );
}
Test it with an Application Password: curl -u "editor:abcd efgh ijkl mnop qrst uvwx" "https://example.com/wp-json/ai/v1/search?q=how+do+I+register+a+block". A copied login cookie on its own will not work: without an X-WP-Nonce header, core treats a cookie request as logged out and the route returns 401. You should get back ranked post IDs with titles, permalinks, and excerpts: never raw HTML, which is why the handler returns wp_trim_words output rather than the full chunk.
The join on wp_posts in the handler is the other half of keeping private content private. Without it, the search scores every stored chunk, including those of posts that were made private or deleted after they were indexed:
A subscriber’s search after an internal post was made private and a long post was deleted: query without the join (test site: WordPress 7.1.3, PHP 8.2; the embeddings endpoint is stubbed because the test site has no internet, so scores come from shared words rather than meaning):
Subscriber searches "salary bands for senior editor" after the post was made private:
0.6325 Private: Q3 salary bands (internal) | Internal salary bands for the editorial team. Senior editor band and contributor rates.
0.1021 Register a custom block | Use block.json and register_block_type to register a custom block for the block editor.
0.0252 | and 9. Option text for command 9. Section 10 of the WP-CLI handbook covers command 10 in detail, with examples and flags. Option text for command 10. Option text for command 10. Option text for command 10. Option text for…
0.0252 The long WP-CLI handbook | and 9. Option text for command 9. Section 10 of the WP-CLI handbook covers command 10 in detail, with examples and flags. Option text for command 10. Option text for command 10. Option text for command 10. Option text for…
0.0000 Schedule cron jobs in WordPress | WP-Cron runs scheduled events when someone visits. Use a system cron for reliable scheduled events.
With the join (same test site):
Subscriber searches "salary bands for senior editor" after the post was made private:
0.1021 Register a custom block | Use block.json and register_block_type to register a custom block for the block editor.
0.0252 The long WP-CLI handbook | and 9. Option text for command 9. Section 10 of the WP-CLI handbook covers command 10 in detail, with examples and flags. Option text for command 10. Option text for command 10. Option text for command 10. Option text for…
0.0000 Schedule cron jobs in WordPress | WP-Cron runs scheduled events when someone visits. Use a system cron for reliable scheduled events.
The blank title in the first output is the deleted post: its rows outlived it.
When search results look wrong
- Empty results after backfill. Check
SELECT COUNT(*) FROM wp_post_embeddings. If it is zero, your API key is missing or the model name is wrong. Look forRAG embed failedindebug.log. - Every save re-embeds. The hash comparison is failing because
post_contentincludes block comments that change on every save. Hash a normalised version:hash( 'sha256', wp_strip_all_tags( $post->post_content ) ). - 429 rate limits during backfill. Reduce the batch size from 32 to 8, or add
sleep( 1 )between batches in the CLI command only. - Search is slow. You are loading every row into memory. Add
LIMIT 5000to the SELECT, or pre-filter bypost_typeandpost_statuswith a join againstwp_posts. - 401 on the REST route. The
permission_callbackis rejecting you. Confirm you are sending a valid nonce (X-WP-Nonce) or a logged-in cookie.
Where to take it next
- Swap in Gemini. The shape is identical: POST to
generativelanguage.googleapis.com/v1beta/models/text-embedding-004:batchEmbedContentsand map the response. Keep the model name in the table so you can migrate without re-embedding everything at once. - Wire it into a block. Register a small block with
register_block_typeand call the endpoint viaapiFetch. Render results withcreateElement: neverinnerHTML. - Add hybrid ranking. Blend the cosine score with a
WP_Querykeyword score for the best of both worlds. - Move to a vector store once you pass ~10,000 chunks. pgvector, Qdrant, or Pinecone all accept the same float arrays you are already generating.
You now have a working RAG pipeline: chunking, hashing, batched embedding, a backfill command, and a capability-checked REST endpoint that ranks by meaning. To let readers ask follow-up questions, feed the results into a streaming chat assistant. For a site with thousands of posts, a resumable WP-CLI backfill with cost guardrails replaces the simple backfill command in step 5.
Leave a Reply