• Hello,

    I am using the Yuto Search plugin and it works great for searching titles and content. Now, I want to enrich the search results by adding the author’s name and the post’s category.
    Do you have, or could you please provide me with a working example of the PHP code needed to add custom data to the index?
    I only have title and excerpt as a results, but really would love to add the category and author name as well

    Thank you very much in advance!

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author ponopress

    (@ponopress)

    Hello @justgert ,

    Apologies for the late response, and thank you for your patience! We’re glad to hear the plugin is working well for your title and content searches.

    Regarding your request — just to confirm, do you want to:

    • Only display the author name and post category in the search results template, or
    • Also make them searchable, so users can query posts by author or category?

    This will help us provide you with the most relevant example for adding custom data to the index and adjusting your templates accordingly.

    Once we know your preference, we can send you a working code snippet tailored for your setup.

    Thanks again for using Yuto Search!

    Regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    Hi there,

    Thanks a lot for your reply. Yuto works great! Results are very ver fast. But now, our results only show the title and a piece of the excerpt.

    Regarding my request:

    The goal is to have both:

    1. Display the author name and category name in the search results.
    2. Also make them searchable, so a query for an author’s name will return their posts, or certain words in certain categories. We have tried a lot to get this working, but for as far as we can see, the author and categories are not in de Index UID.

    A working code snippet that demonstrates how to add these custom attributes to the index using the correct PHP filter would be perfect. Thank you!

    Looking forward to your reply.
    Best Regards,

    Gert

    Plugin Author ponopress

    (@ponopress)

    Hi @justgert ,

    First of all, apologies for our delayed response — this query required a bit more work to ensure it was handled correctly, so it took us some extra time. We truly appreciate your patience.

    We’re happy to let you know that this is now resolved and included in the latest plugin release (v0.1.3). You can now add custom attributes like author names and categories to the index.

    Regarding PHP hooks, since the plugin is made to be more compatible with the new Editor, it’s mostly JS based and provides WordPress JS hook than PHP hooks.

    To make things even easier, we’ve also created a step-by-step blog tutorial showing exactly how to enqueue your JS, modify document data, re-index posts, and display these new attributes on the frontend:
    How to Add Custom Attributes (Author, Category, Tags) to the Index and Show Them

    Thank you again for submitting this query — it helped us improve the plugin and provide a clearer, more flexible solution for adding custom attributes. We hope the tutorial and v0.1.3 will make it easy for you to implement exactly what you need.

    Best regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    Hi there and thanks for your reply.

    Following up on my previous request, I’ve updated to v0.1.3, but the indexing of custom attributes is still failing.

    I have found a specific error in my debug.log that seems to be the root cause. It appears every time I try to re-index:

    PHP Warning: Undefined array key "adminAPIKey" in /wp-content/plugins/yuto/inc/admin/setting.php on line 157

    This seems to be a bug within the plugin, as it cannot find its own Admin API Key in the settings, which likely breaks the entire indexing process. Could you please look into this?

    Thank you.

    Plugin Author ponopress

    (@ponopress)

    Hello @justgert ,

    Thank you for following up and sharing the debug log details.

    The adminAPIKey should already be set, as it’s required for the indexing process in the backend. This key is saved when you connect to your Meilisearch server from the Connection settings in the plugin. Could you please try reconnecting there to ensure the key is properly saved?

    If the issue persists after reconnecting, please let us know and provide any additional details you can, so we can investigate further and get this resolved for you.

    Best regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    Hello,

    Thank you for your suggestion to reconnect. I have done that, and it successfully connected.

    However, the two core problems remain, even after updating to v0.1.3 and reconnecting:

    1. The adminAPIKey warning is still present. Every time I re-index, the following PHP Warning is logged in my debug.logPHP Warning: Undefined array key "adminAPIKey" in /wp-content/plugins/yuto/inc/admin/setting.php on line 157

    2. Custom attributes are still not being indexed. I have followed your new tutorial exactly. I created the custom-admin-scripts.js file and used the PHP snippet to enqueue it. I even used the minimal example code from your tutorial to only add categories.

    After a full “Clear and Re-index”, the fieldDistribution for my posts index still only shows the default fields. The custom attributes are completely missing: "fieldDistribution":{"content":27004,"featured_media_url":27004,"id":27004,"link":27004,"strippedContent":27004,"title":27004}

    This seems to confirm that the JavaScript hooks (yuto_modify_documents_data, etc.) are not being executed or applied during the indexing process, and the adminAPIKey issue seems to be a related bug in the plugin’s backend.

    Could you please look into this? I am ready to provide any other information you need.

    Thanks!
    Best regards,
    Gert

    Plugin Author ponopress

    (@ponopress)

    Hello @justgert ,

    Thank you for the detailed response.

    1. We’ve also been able to replicate the adminAPIKey issue on our side. It looks like on the initial connection the Admin APIkey isn’t being set properly, as the API Key isn’t yet available on the first load. Re-connecting should usually resolve this. That said, we are actively working on a fix so this won’t affect future setups, and this should not impact the current issue you’re facing.
    2. In the meantime, could you please share the full code so we can verify it for you and where you are using that code? Also, after enqueuing custom-admin-scripts.js, please confirm that it’s loading on the plugin’s settings page by checking the page source.

    Once we have this information, we’ll investigate further.

    Regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    Hi,

    Thank you for your quick response and for confirming the adminAPIKey issue.

    You asked for the full code I am using and confirmation that the custom JavaScript file is loading. I have tested this, and the core of the problem seems to be that the JavaScript file is not being loaded on the settings page.

    Here is the complete code I am using, based on your tutorial:

    This is a Code Snippet, set to run in the admin area, to enqueue the script.

    PHP

    /**
     * Load the custom JavaScript config file on the Yuto settings page.
     */
    add_action('admin_enqueue_scripts', 'gb_enqueue_yuto_admin_scripts');
    function gb_enqueue_yuto_admin_scripts($admin_page_hook_suffix)
    {
        // I have tried both a strict check and this broader strpos check.
        // Neither seems to trigger.
        if ( strpos($admin_page_hook_suffix, 'yuto-settings') !== false ) {
            wp_enqueue_script(
                'gb-yuto-admin-config',
                get_stylesheet_directory_uri() . '/js/yuto-custom-config.js',
                ['wp-hooks', 'wp-api-fetch', 'yuto-js'],
                '1.3.0',
                true
            );
        }
    }
    

    2. The JavaScript Code (/js/yuto-custom-config.js)

    This is the content of my yuto-custom-config.js file. It’s the minimal test case from your tutorial, which only tries to add categories.

    JavaScript

    document.addEventListener("DOMContentLoaded", function () {
    	// Helper function from your tutorial
    	const getPostTermNames = async (term = "categories", postId) => {
    		try {
    			const terms = await wp.apiFetch({
    				path: /wp/v2/${term}?post=${postId}&per_page=100,
    			});
    			const termNames = terms.map((term) => term.name);
    			return termNames;
    		} catch (error) {
    			console.error("Error fetching terms:", error);
    			return [];
    		}
    	};
    
    	// Function to modify the data
    	const my_prefix_modify_documents_data = async (data, post, UID, restBase) => {
    		if ("posts" == restBase) {
    			const categoriesNames = await getPostTermNames("categories", post.id);
    			data["categories"] = categoriesNames;
    		}
    		return data;
    	};
    
    	// Function to update the settings
    	const my_prefix_change_searchable_attributes = (searchableAttributes, UID) => {
    		searchableAttributes = ["title", "strippedContent", "categories"];
    		return searchableAttributes;
    	};
    
        const my_prefix_change_filterable_attributes = (filterableAttributes, UID) => {
            filterableAttributes = ["categories"];
            return filterableAttributes;
        };
    
    	// Applying the filters
    	wp.hooks.addFilter("yuto_modify_documents_data", "my_namespace", my_prefix_modify_documents_data);
    	wp.hooks.addFilter("yuto_searchable_attributes", "my_namespace", my_prefix_change_searchable_attributes);
        wp.hooks.addFilter("yuto_filterable_attributes", "my_namespace", my_prefix_change_filterable_attributes);
    });
    

    Summary of the Problem:

    The issue seems to be that the admin_enqueue_scripts hook in my PHP code is not correctly identifying the Yuto settings page. The page hook for me is toplevel_page_yuto-settings, but even a direct comparison fails. As a result, the yuto-custom-config.js file is never enqueued, and the JavaScript hooks are never applied.

    Could you please provide the definitive, correct way to enqueue a script specifically on your settings page? It seems the page hook is not behaving as expected in my environment.

    I’ve also tried the exact code found on this page:
    https://ponopress.com/2025/08/22/how-to-add-custom-attributes-author-category-tags-to-the-index-and-show-them/

    So far no luck. To be clear: When I visit the settings in wp admin I see the host URL and API Key, they are visible and even used the CONNECT button several times to make sure the settings are saved. Performing a search in the front-end works very good and is very very fast. Only problem is we cannot add the author and categories to the index UID.

    If you need more information then please let me know. Thanks for your help! Looking forward to your reply
    best regards,
    Gert


    Plugin Author ponopress

    (@ponopress)

    @justgert

    Thank you for bringing this to our attention, and we truly appreciate your patience in working through this issue. Upon reviewing the situation, we found that the issue stems from an incorrect script handle name in the guides and blogs we previously shared. Specifically, the script handle for the Yuto JavaScript was mistakenly referred to as yuto-js in the dependencies array, which is incorrect. The correct handle should simply be yuto.

    We will be updating the documentation to reflect the correct code shortly. In the meantime, here’s the corrected code to properly enqueue the custom JavaScript (yuto-custom-config.js) on the Yuto settings page:

    add_action('admin_enqueue_scripts', 'gb_enqueue_yuto_admin_scripts');
    function gb_enqueue_yuto_admin_scripts($admin_page_hook_suffix)
    {
    // We use strpos to check if we are on the Yuto settings page.
    if ( strpos($admin_page_hook_suffix, 'yuto-settings') !== false ) {
    wp_enqueue_script(
    'gb-yuto-admin-config',
    get_stylesheet_directory_uri() . '/js/yuto-custom-config.js',
    ['wp-hooks', 'wp-api-fetch', 'yuto'],
    '1.3.0',
    true
    );
    }
    }

    This change should ensure that the categories are correctly indexed, and you can then display them on the front-end as described in the blog tutorial.

    It is unfortunate that such a small oversight led to this delay, but we want to sincerely thank you for your feedback, it’s helping us improve the process and documentation for all users. We’ll be updating the guide and blog post shortly to reflect this change.

    If you need any further clarification or assistance with the implementation, please don’t hesitate to reach out. We are here to help!

    Regards,

    Pono Press

    Plugin Author ponopress

    (@ponopress)

    Hi @justgert ,

    We just wanted to follow up regarding the solution we shared earlier. We haven’t heard back from you yet, so we wanted to check in — did that resolve the issue on your end?

    If everything is working now, that’s great to hear. If not, please let us know the current status or share any new details so we can continue troubleshooting with you.

    Looking forward to your update.

    Best regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    Hello,

    Thank you for your previous help. Your suggestion to change the dependency handle to 'yuto' was the key!

    Good news: I can confirm that with the corrected code, the categories are now being successfully indexed. This proves the JavaScript hooks are now working.

    However, two issues remain:

    1. The author_name is still not being indexed. Even when I use a similar wp.apiFetch call for the author, this specific attribute is not added to the index, while categories and tags are.

    2. A critical performance issue. During the re-indexing of my 27,000+ posts, the server CPU load spikes to over 200%. Up to a load of +/- 70 and ran out of CPU. This is because the client-side JavaScript method makes tens of thousands of individual API calls back to my own server. It’s essentially a “self-DDoS”. This approach is not scalable for large websites.

    My questions:

    1. Could you please provide a definitive, complete example yuto-custom-config.js that correctly adds both categories and author_name, so I can rule out any error on my part?
    2. More importantly: is there a server-side PHP filter (like yuto_document_map or similar) available in v0.1.3? A PHP-based method would be infinitely more efficient and would solve the server load issue completely.

    The client-side API call method is unfortunately not a workable long-term solution for a site of this size. A server-side PHP filter would be the perfect solution.Thank you again for your excellent support.

    Best regards,
    Gert

    Plugin Author ponopress

    (@ponopress)

    Hello @justgert ,

    That’s great news — thank you for confirming!
    We’re glad the dependency handle adjustment did the trick and that categories are now indexing properly.

    1. Below is a complete example of the yuto-custom-config.js file which you can load on the admin side. It’s also up to you whether you’d like to include author_name (and categories) as part of your searchable attributes or show it on the frontend template.
    document.addEventListener("DOMContentLoaded", function () {
    wp.hooks.addFilter(
    "yuto_searchable_attributes",
    "your_namespace",
    your_prefix_change_searchable_attributes,
    10,
    );
    function your_prefix_change_searchable_attributes(
    searchableAttributes,
    UID,
    ) {
    searchableAttributes = ["categories"];
    return searchableAttributes;
    }

    // Fetch term names (categories or tags) for a specific post
    const getPostTermNames = async (term = "categories", postId) => {
    try {
    // Fetch the term objects (categories/tags) for the given post ID
    const terms = await wp.apiFetch({
    path:
    /wp/v2/${term}?post=${postId}&per_page=100,
    });

    // Map the term objects to extract only the name of each term
    const termNames = terms.map((term) => term.name);

    return termNames;
    } catch (error) {
    console.error("Error fetching the terms:", error);
    return [];
    }
    };

    // Fetch the author name for a specific post
    const getPostAuthorName = async (postId) => {
    try {
    // First, fetch the post to get the author ID
    const post = await wp.apiFetch({
    path: /wp/v2/posts/${postId},
    });

    const authorId = post.author;

    if (!authorId) {
    return "";
    }

    // Fetch the author user object
    const author = await wp.apiFetch({
    path: /wp/v2/users/${authorId},
    });

    return author.name || "";
    } catch (error) {
    console.error("Error fetching the author:", error);
    return "";
    }
    };

    // Modify document data for a post to include category and tag names
    const your_prefix_modify_documents_data = async (data, post, UID, restBase) => {
    if ("posts" == restBase) {
    // Fetch and add category names to the data
    const categoriesNames = await getPostTermNames("categories", post.id);
    data["categories"] = categoriesNames;

    // Fetch post author and add to the data
    data["author_name"] = await getPostAuthorName(post.id);
    }

    return data;
    };

    // Add 'categories' and 'tags' to filterable attributes for posts
    wp.hooks.addFilter(
    "yuto_modify_documents_data",
    "your_namespace",
    your_prefix_modify_documents_data,
    );
    });

    2. Thank you for raising this important point. You’re absolutely right — for large sites, relying solely on client-side API calls can add unnecessary load and isn’t the most efficient long-term solution.

    In the meantime, we do provide a WP-CLI command that you can use to reindex and manage documents directly from the server. This can help bypass some of the limitations of client-side calls and is much more efficient for larger sites.

    Please don’t hesitate to reach out if you run into anything else or have further ideas — your feedback helps us keep improving the plugin.

    Regards,
    Pono Press

    • This reply was modified 4 months, 1 week ago by ponopress.
    Thread Starter justgert

    (@justgert)

    Hello,

    Thank you for your continued support. I have completed my testing and wanted to provide you with my final feedback on the indexing process for large sites (27,000+ posts) with version 0.1.3.

    First, the good news: I can now successfully index custom attributes like categories and tags, and authors. So the base functionality of the JavaScript hooks is working. However, there are two critical issues with the available indexing methods.

    1. The Client-Side (Browser) Indexing Method:

    • Problem: When indexing from the admin page, my server’s CPU load is constantly at 100% and the server load average goes up to 30+.
    • Symptom: The browser console shows over 2,000+ network errors (net::ERR_FAILED and net::ERR_CERT_VERIFIER_CHANGED).
    • Diagnosis: The client-side method, which makes thousands of individual wp.apiFetch calls, is creating a “self-DDoS” effect. My server’s security and rate-limiting measures are triggered, which causes many of the API calls to fail. This results in an incomplete index where some posts have the custom attributes, and many do not. Hit and miss here.

      Also the Undefined array key “adminAPIKey” in /www/wwwroot/www.mydomain.ext/wp-content/plugins/yuto/inc/admin/setting.php on line 157 is still present. Even after saving the settings again.

    2. The Server-Side (WP-CLI) Indexing Method:

    • Problem: When I try to use the wp yuto build-index posts command as you suggested, the process fails with a fatal PHP error.
    • Symptom: The command aborts after encountering this error: PHP Fatal error: Uncaught TypeError: ftp_pwd(): Argument #1 ($ftp) must be of type FTP\Connection, null given in /wp-admin/includes/class-wp-filesystem-ftpext.php:230
    • Diagnosis: The stack trace shows this error is triggered by a conflict with the a populair Member plugin, which makes an incompatible call to the WordPress Filesystem API within the WP-CLI environment.

    Conclusion & Suggestion: Based on this, it seems both currently available methods are not viable for a large, production website with other complex plugins:

    • The client-side method is not scalable and causes server overload and an incomplete index.
    • The server-side WP-CLI method is not robust enough and can be broken by conflicts with other popular plugins.

    The most stable, efficient, and professional solution for large sites would be a server-side PHP filter (like yuto_document_map). This method would enrich the data in a single, fast PHP loop, avoiding both the server overload and the WP-CLI conflicts.

    Could you please consider re-implementing or fixing the server-side PHP filters in a future update? For users with large websites, this seems to be the only truly reliable path forward.Thank you again for your time and the great plugin.

    Edit: typo

    • This reply was modified 4 months, 1 week ago by justgert.
    Plugin Author ponopress

    (@ponopress)

    Hello @justgert ,

    Thank you so much for taking the time to run these tests and share such a detailed report, it’s extremely valuable for us. We’re glad to hear that indexing custom attributes like categories, tags, and authors is working well for you.

    Your conclusion is very insightful, and we agree: a server-side PHP filter approach would indeed be the most stable and professional solution for large sites. It avoids both the server overload of the browser method and the brittleness of WP-CLI conflicts.

    We are planning to move in this direction. That said, this transition will take some time and research to ensure it’s smooth; especially since we need to carefully bridge the gap between the current client-based approach and the future server-based implementation. Our goal is to make this change as seamless as possible so it doesn’t break existing workflows.

    Thank you again for your patience, your detailed feedback, and for using Yuto on such a large-scale project. Feedback like yours is what helps us prioritize improvements and build a more robust plugin for everyone.

    Regards,
    Pono Press

    Thread Starter justgert

    (@justgert)

    No problem, we both benefit from this 🙂
    Okay, let me know when things have changed in the future so I can test again. For now I have to keep the search results without the category and author.

    Be good and success with further development!
    regards,
    Gert

Viewing 15 replies - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.