Back to Blog
How Queryra Works Under the Hood: WordPress Hooks Explained
technical

How Queryra Works Under the Hood: WordPress Hooks Explained

A technical deep dive into how Queryra replaces WooCommerce search using WordPress hooks — without touching your theme or breaking other plugins.

RG
Rafal Gron
Founder, Queryra
February 6, 2026·8 min read

One of the most common questions from developers evaluating Queryra: "How does it replace WordPress search without breaking my theme or other plugins?"

The answer is WordPress hooks — specifically pre_get_posts and posts_search, the two filters that let a plugin take over what matches a search query without touching core, your theme, or any other plugin.

This article is a technical walkthrough for developers, agency teams, and anyone curious about how an external AI service integrates cleanly with WordPress's search infrastructure. The code samples below are illustrative — simplified for clarity, not copy-paste from the plugin.

The WordPress Hook System for Search

WordPress provides several filter hooks that let plugins modify search behavior without touching core files or themes:

pre_get_posts — Fires before the query runs. The right place to inspect the search term and decide what to do.

posts_search — Filters the WHERE clause of the search SQL. This is where you control what matches a search query.

WordPress also exposes posts_orderby (filters ORDER BY) and posts_clauses (filters all SQL clauses at once). Some search plugins lean on those for ranking. Queryra doesn't — instead, it preserves a stable order via IN (id1, id2, …) directly in the WHERE clause, which keeps the SQL simpler and avoids fighting with other plugins that may already filter ORDER BY.

These hooks are why WordPress's plugin ecosystem works so well for search — any plugin can replace search behavior without modifying core files, your theme, or other plugins.

How Queryra Uses These Hooks

Queryra's WordPress plugin intercepts the search flow at two points:

Step 1: Catch the search and call the API (pre_get_posts)

When WordPress detects a search, the plugin fires on pre_get_posts. It takes the search query string, sends it to the Queryra API, and receives back an ordered list of post IDs ranked by semantic relevance. The IDs are cached for the rest of the request lifecycle so the API isn't called twice.

add_action('pre_get_posts', function ($query) {
    if (!is_admin() && $query->is_search() && $query->is_main_query()) {
        $term = $query->get('s');
        $ids  = queryra_api_search($term); // returns ordered post IDs
        // stash for the posts_search filter below
        $GLOBALS['queryra_ids'] = $ids;
    }
});

Step 2: Replace the WHERE clause and preserve order (posts_search)

Instead of the default LIKE '%keyword%' match, the plugin replaces the search WHERE clause with an ID IN (...) against the AI-ranked list. The order from the API is preserved by emitting the IDs in their ranked sequence — WordPress then renders results in that exact order without needing a separate ORDER BY override.

add_filter('posts_search', function ($search, $query) {
    if ($query->is_search() && $query->is_main_query() && !empty($GLOBALS['queryra_ids'])) {
        $ids = array_map('intval', $GLOBALS['queryra_ids']);
        if (empty($ids)) {
            return ' AND 1=0 '; // no matches — return empty
        }
        return ' AND wp_posts.ID IN (' . implode(',', $ids) . ') ';
    }
    return $search;
}, 10, 2);

That's the entire search-side integration: one action, one filter. WordPress still handles pagination, template rendering, the WP_Query object, and every downstream plugin's hook chain — Queryra just changes what matches.

Why This Approach Doesn't Break Things

Hook-based search replacement is safe because:

Your theme stays untouched. Queryra doesn't modify search.php, archive.php, or any template file. It only changes what posts WordPress retrieves — the theme renders them exactly as before.

Other plugins keep working. WooCommerce product filters, breadcrumbs, pagination, and SEO plugins all work normally because WordPress's query structure is preserved. Only the matching logic changes.

Graceful fallback. If the Queryra API is unreachable (network issue, server down), the plugin falls back to default WordPress search. The customer still gets results — just keyword-based instead of semantic.

No database modifications. Queryra doesn't add tables, modify existing tables, or store anything in your WordPress database (except the API key in options). Uninstalling is clean.

WP_Query compatibility. Because results come back as standard WordPress post IDs, everything that depends on WP_Query works: the_title(), the_permalink(), get_the_post_thumbnail(), custom fields, ACF fields — all normal.

How Product Sync Works

For semantic search to work, Queryra needs to know about your products. The sync process works like this:

  1. Initial sync — When you click "Sync" in plugin settings, the plugin collects all published products (or posts/pages depending on your config) including titles, descriptions, excerpts, prices, attributes, and selected metadata.
  1. API upload — Product data is sent to the Queryra API via HTTPS. The API processes each item through a sentence-transformer model, generating a vector embedding that captures the semantic meaning of the content.
  1. Auto-sync on save — The plugin hooks into save_post (and the matching delete action). When you publish, update, or delete any post type that's in scope (products, posts, pages), it automatically syncs that single item to Queryra. No manual re-sync needed for incremental changes.

add_action('save_post', function ($post_id, $post, $update) {
    if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
        return;
    }
    if ($post->post_status !== 'publish') {
        return;
    }
    // delegate to plugin's sync routine
    Queryra_Sync::sync_post($post_id);
}, 10, 3);

Note the save_post action (not save_post_{type}) — one hook covers products, posts, pages, and any custom post type you've enabled in settings.

  1. Vector storage — Embeddings are stored on Queryra's infrastructure. Your WordPress database isn't modified beyond the plugin's own settings option.

Developer Integration Points

If you're building custom integrations with Queryra, here are the key touchpoints:

REST API — For headless WordPress, custom frontends, or non-WordPress sites, the Queryra REST API can be called directly. Send a query, receive ranked results with scores. See the REST API guide.

WP-CLI — Plugin sync and management can be triggered from the command line, useful for large catalogs and automated deployments.

Standard WordPress hooks — Because the plugin works through pre_get_posts and posts_search, anything you'd normally hook into WordPress search continues to work: WP_Query results behave normally, and your theme's search.php template renders them as-is. Templates, breadcrumbs, pagination, layered nav — none of it cares that the matching logic was swapped underneath.

The plugin source is on GitHub — read the actual hook signatures there if you need exact priority or argument shapes for a custom integration.

Ready to fix your WooCommerce search?

Open source WordPress plugin

Frequently Asked Questions

Does Queryra modify my WordPress theme files?

No. Queryra uses WordPress filter hooks (posts_search, posts_orderby) to modify search results without touching any theme files. Your search.php template renders results exactly as before — only the products returned change.

Will Queryra conflict with other WordPress plugins?

Queryra is designed to be compatible with other plugins. It only modifies the main search query using standard WordPress hooks. WooCommerce, Yoast SEO, WPBakery, Elementor, and other major plugins work normally.

What happens if the Queryra API is down?

The plugin falls back to default WordPress search automatically. Your store's search keeps working — it just uses keyword matching instead of semantic search until the API recovers.

Does Queryra add tables to my WordPress database?

No. Queryra stores only your API key in WordPress options. All vector embeddings and search processing happen on Queryra's infrastructure. Uninstalling the plugin leaves your database clean.

Can I use Queryra with a headless WordPress setup?

Yes. Queryra's REST API can be called directly from any frontend — React, Vue, Next.js, or custom applications. You don't need the WordPress plugin for headless setups.

Related Reading