Skip Navigation

[Resolved] Saving searches

This support ticket is created 2 years, 11 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.

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: Africa/Casablanca (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Simon Logan 2 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2047625

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?

#2048203

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

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.

#2051727

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?

#2052009

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

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 );
#2052057

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 🙂

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