Why add custom attributes?
Search is often more than just finding words in a title or content. In real-world sites, users want to search by categories, see who wrote a post, or even browse by tags. That’s why extending the search index with custom attributes is so valuable — it makes results richer and more useful.
In fact, one of our users recently asked for this feature on the WordPress.org support forums: How to add custom attributes (author, category) to the index?. That discussion inspired us to write this guide and show exactly how you can do it using the new yuto_modify_documents_data hook in Yuto Search.
Step 1: Enqueue Your Custom JavaScript File
Since Yuto Search exposes WordPress-style JavaScript hooks, the first step is to load your own JS file into the admin so you can hook into them. You’ll do this by enqueuing a custom script in your plugin or theme.
Here’s an example in PHP:
Once this is enqueued, you’re ready to start writing JavaScript that interacts with Yuto’s indexing process.
Step 2: Write the Hook in JavaScript
Now that your custom-admin-scripts.js file is loaded into the admin, you can use WordPress’ JS hooks to modify the data before it’s sent to Meilisearch. The new filter yuto_modify_documents_data introduced in 0.1.3 lets you add or change attributes for each document being indexed.
Since you want the result to be searchable by categories, you need to add it to the searchable attributes using the hook yuto_searchable_attributes
Here’s a simple example that adds the categories of a post:
Step 3: Re-index Your Data
Because the hook runs inside the WordPress admin, it only affects the data at the moment a post is indexed (on save, update, or delete). That means your existing content won’t magically get the new attributes—you’ll need to re-index it.
👉 After re-indexing, head over to your Meilisearch dashboard (or use the API) to confirm that your documents now contain the new fields.
Step 4: Show Custom Attributes in Frontend Search Results
Once your documents are re-indexed with the new attributes, the next step is to display them in your search results. You can customize the item templates to include categories, tags, or author names.
This JS file don’t need to be enqueued on the admin. You can enqueue like any normal frontend JS file.
Here’s a simple example for it:
With this final step, your search results are now fully enriched, showing categories custom attribute you added via the hook. You can do the same for other attributes like: tags, authors and more.
Summary
By using the new yuto_modify_documents_data hook, you can easily enrich your Meilisearch index with custom attributes like author names, categories, tags, or any other metadata. The process is simple:
- Enqueue a custom JS file in the admin with
wp-hooksandwp-api-fetchdependencies. - Write a hook in JS to modify the document data before it’s indexed.
- Re-index your content so existing posts include the new attributes.
- Customize your frontend templates to display the enriched data in search results.
This approach gives you full control over your search data, enabling more meaningful results, and a richer user experience. Whether you want users to see authors, search by category, or explore tags, Yuto makes it easy to extend the index exactly the way your site needs.
