Skip Navigation

[Resolved] How to display Category and Subcategory search terms in an AJAX view?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 2 replies, has 2 voices.

Last updated by Kirill 3 years, 4 months ago.

Assigned support staff: Minesh.

Author
Posts
#530132

I have a view with multiple food items. Each item belongs to category and a subcategory (i.e. Beverages→Tea, Beverages→Coffee etc.). With Views I've created a search form that selects all items from particular category and a subcategory. This view is uses AJAX to update it's content. Works really great!

What I need to do is to display a search category and a subcategory inside a page title. So if someone searched for 'Tea' and selected 'Beverages→Tea' in a search dropdown, a title would show 'Beverages: Tea'.

Since it uses AJAX I think that any PHP techniques (i.e. shortcodes) are out of use here. So I think it has to be a JS-only solution.

What I'm planing is:
1. Determine what category slug was searched by
1a. Checking search drop down for options with 'selected' attribute, or, probably better yet
1b. Parse a URL string for a URL search parameter to figure out what term was searched for.
2. Using WP REST API to retrieve a list of all terms for a category. Scan the list for the slug that was searched. Check if this slug has any parent items, and if it does, retrieve those parent items. Continue until the very top parent item is found.
3. Use JS to replace page title text with parent and child categories.
4. Repeat every time a search is executed.

Is this the right approach to achieve what I want? Are there any better or simpler ways to achieve the same?

Would be really grateful for a good advise here.

Best regards,
Kir

#530316

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Views offer ajax callback hooks that may you would like to use here and follow use the JS:

Edit your view and you need to open the JS editor under the "Filter section" main editor and click the Frontend events button:
=> hidden link

Select "Custom Search" tab and further select checkbox "The custom search results have been updated":

You will see the following code added to editor once you select the checkbox - for example:

jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
    /**
    * data.view_unique_id (string) The View unique ID hash
    * data.layout (object) The jQuery object for the View layout wrapper
    */
     
});
#530529

Thank you very much, Minesh!
This is exactly what I need.

May I have a suggestion for some time in the future to pass search terms as parameters to the data object of a callback function? This will allow to avoid parsing the URL and be future proof in case URL parameters format change. I think it shall not be hard to implement.

Thanks!