What we are hoping to do is to be able to save the searches performed on our site via a particular Toolset form for example the one at hidden link.
So my aim is that when someone performs a search, as well as the search itself running as normal, the criteria of their search are also recorded and stored, I would imagine in a custom post type which I could set up - do you know if this would be possible? Perhaps via a hook/filter which can run on the form search execution?
My thinking is that I'd create a new custom post type, add in fields for each of the search criteria (Loan type, asset class, location etc.), then when a search is performed have this information create a new post of this custom post type using the values searched upon? This would happen in the background but means that we'd have an archive of searches performed on the site and would be able to access and present that information as required?
Hello and thank you for contacting the Toolset support.
Currently, there is no action in Toolset hooks that are documented to be used in a similar case. But you can still use filters. For example, you can use the wpv_filter_query filter, check the underlying WP_Query arguments, and if you found your filter arguments you can run your code to save the search.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Something like:
add_filter( 'wpv_filter_query', 'my_track_searches', 10, 3 );
function my_track_searches( $query_args, $view_settings, $view_id ) {
if ( $view_id == 1234) {
$data = [];
$loanType = $_GET['wpv-wpcf-finance-loan-type'];
if ( $loanType ) $data['loan-type' => $loanType];
// ... get all the filters.
track_search( $data );
}
// return the $query_args, so the view can continue generating.
return $query_args;
}
You may also want to use a different hook, such as wpv_filter_query_post_process which is run after the view gathers results from the database.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
I hope this helps. Let me know if you have any questions.
Hi Jamal, thanks for this initial guidance.
So I've created a custom post type which will store the data (as this means we can then extract data from the searches, such as showing the most recent 5 searches or all searches by a certain user), the next step is to get the site to create a new post of this type whenever a search is performed, using the details they searched on.
From your code, I presume that the value of "view_id" should be that of the Toolset View? And it looks like your code looks for the values and stores them in the array $data. Do I then just need to add in additional code to take those values and create a new post using them?
I presume that the value of "view_id" should be that of the Toolset View?
You are right. That way, the code will be run only for that view, and not for every view.
And it looks like your code looks for the values and stores them in the array $data.
Yes. Usually, you would like to gather all the data, validate it for inserting it.
Do I then just need to add in additional code to take those values and create a new post using them?
Yes. I added track_search( $data ); as an example of the code that will insert this data into the database. "track_search" is not an actual function. I only added it for demonstation.
What you will use is WordPress functions:
- https://developer.wordpress.org/reference/functions/wp_insert_post/
- https://developer.wordpress.org/reference/functions/update_post_meta/
Keep in mind that Toolset custom fields use a "wpcf-" prefix. For a custom field "email", you should use "wpcf-email" like:
update_post_meta($post_id, 'wpcf-email', $email );
Thanks Jamal, you'll be glad to hear that I managed to solve this issue using your guidance as a starting point.
In the end I simplified things a bit, rather than creating the array then passing those values to another function to add the post, since I didn't need that function elsewhere (yet!) I inserted an update_post code block then used the update_meta to grab the values from the URL and insert them all in one go.
Works a treat and it's good to continue to expand my knowledge of PHP, WordPress and Toolset 🙂