Skip Navigation

[Resolved] Search History

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 0.13 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#1519769

Tell us what you are trying to do?

I would like to have a section or view that shows the search history of each user. so the user can go back the search result with a click of button.
Basically a log of he or she searched on the search box on my site with links.

Is there any documentation that you are following?

no

Is there a similar example that we can see?

What is the link to your site?

hidden link

#1521831

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I'm afraid, this feature is not available out-of-the-box, but you're welcome to submit a feature request for it, at:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

A workaround can be to use a custom user field and a user edit form to allow users to save a specific search if or when needed.

Note: For this approach to work, it is important that your view's search form is not set to update results using AJAX and the page is reloaded with each search.

1. You can add a new user field named "User Searches" and set it to have multiple instances, using the "Allow multiple instances of this field" option.
Example screenshot: hidden link
( ref: https://toolset.com/documentation/user-guides/custom-content/user-fields/ )

2. Next, you can create a user edit form and set it to edit an existing user.

Example screenshot: hidden link

3. That form will need only one hidden field, with field slug "current-query-string" and default field value as "[get_current_query_string]".

Example screenshot: hidden link.

3. You'll also need to register a custom shortcode that can get the search query filter parameter part from the current URL and use it as a value of the hidden field, mentioned in the last step:

 

add_shortcode('get_current_query_string', 'get_current_query_string_fn');
function get_current_query_string_fn() {

	return $_SERVER['QUERY_STRING'];

}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

4. You'll also need to attach a custom function to "cred_save_data" hook, which can save the search query filters from the hidden field as a value of the custom user field "User Searches".
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )

Example:


add_action('cred_save_data', 'my_save_data_action_user',10,2);
function my_save_data_action_user($new_user_id, $form_data)
{
	if ( ($form_data['id']==123) && (!empty($_POST['current-query-string'])))
	{ 
		add_user_meta($new_user_id, 'wpcf-user-searches', $_POST['current-query-string']);
	}
}

Please replace 123 with the actual ID of your user form, and again the above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

5. The last step would be to include this new form, above or below the search form of your view.

The final outcome would be that the user will perform a search and if he/she would like to save it for later use, the submit button for the user form can be clicked and that search's query string will be saved.

You'll be able to show these saved search query strings from the user custom field, using the Type Fields API:
https://toolset.com/documentation/customizing-sites-using-php/functions/

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1561695

Thank you so much for the directions and custom code, Waqar!
It was such a nice of you for providing the information.

I appreciate the workaround, but I was looking something automatic. so every time your search some keyword (ideally other parameters like pulldown filter selections too) and when the user sees the list of search history, with one click he or she can see the same result.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.