Skip Navigation

[Resolved] Custom search and filter with REST API

This support ticket is created 2 years, 10 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+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by danielJ-10 2 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2106049

I'm trying to create a custom rest endpoint that filters through all the users based on toolset custom fields.

I was checking how to create a view for custom search that can be filtered https://toolset.com/course-lesson/creating-a-custom-search/ and I was thinking that might be possible to be able to use the internal functions that make that view to work but return the values in the rest endpoint instead.

Please let me know if this is possible or which functions/hooks should I be looking at to accomplish this functionality.

#2106065

Jamal
Supporter

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

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

Hello and thank you for contacting Toolset support.

If you are looking to use the internals of Toolset views to get the results of a certain view as a PHP array, check the get_view_query_results
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

Check our current integration here https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/

To implement a custom REST endpoint, check the WordPress REST API documentation.
https://developer.wordpress.org/rest-api/extending-the-rest-api/

Keep in mind that Toolset views uses WordPress queries classes under the hood:
- User view will use WP_User_Query https://developer.wordpress.org/reference/classes/wp_user_query/
- Post view will use WP_Query https://developer.wordpress.org/reference/classes/wp_query/
- Taxonomy view will use WP_Term_Query https://developer.wordpress.org/reference/classes/wp_term_query/

I hope this helps. Let me know if you have any questions.

#2106067

Thank you for all the resources, I will look deeper into them. Another question I have is that, currently I implemented Toolset blocks to develop certain features. But, Toolset Blocks works with the get_view_query_results function? Or there is something specific for toolset blocks?

#2106069

Also, how should I implement a Toolset Block if I want to use users?

#2106071

Jamal
Supporter

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

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

Well, the blocks editor does not support users or taxonomy views yet. It only supports posts views.

If you want to use a user view with get_view_query_results, it must be implemented with the legacy editor. Which you can activate in Toolset->Settings->General(tab).

You can reuse the views(post views) that are created in blocks. But you will have to detect their ID. The only way to do so is by checking the generated page HTML source code. The view has its ID, in the output markup(id, class, data-*).

Personally, if I am developing a REST endpoint I would not reuse Toolset functions, I will directly use WordPress internals, keeping in mind that a custom field has the "wpcf-" prefix. For example, an email field with slug "email" would be saved in the database as "wpcf-email".
To get its value for a post, I'll use:

get_post_meta( $post_id, 'wpcf-email', true);

Check this article https://toolset.com/documentation/customizing-sites-using-php/functions/

#2106079

Thank you, I actually agree. The problem and the main reason I'm asking is that, if I can reuse the functions that Toolset already implements to get the users, filter them and so on, I can maybe just pass the values I need and get the response, therefore, saving time. Also, I tried the function you are mentioning (types_render_field) but it is not working when I'm trying to display checkboxes value (since they are coming as example: wpcf-fields-checkboxes-option-ef71de9a49574f914819a39c2f7d2c95-1)

#2106101

So, how can I do a a WP_User_Query for checkboxes? I'm trying the next but no results so far:

'relation' => 'AND',
				[
					'key' => 'wpcf-artists-medium',
					'value' => 'wpcf-fields-checkboxes-option-ef71de9a49574f914819a39c2f7d2c95-1',
				]

look's like that since the artists medium custom field is a nested array I cannot access it individually to them to filter based on their selected checkboxes.

#2106451

Jamal
Supporter

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

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

Well, the checkboxes field is a special field, because the user's selection is stored as a serialized array.
hidden link

With checkboxes fields, you need to keep in mind two things to be able to use them on search:
- The field needs to be configured to store 1 for selected values and none for non-selected values. Otherwise, the search will return unexpected results. Check this screenshot hidden link
- Use the LIKE operator in the query arguments:

'relation' => 'AND',
                [
                    'key' => 'wpcf-artists-medium',
                    'value' => 'wpcf-fields-checkboxes-option-ef71de9a49574f914819a39c2f7d2c95-1',
                    'compare' => 'LIKE',
                ]

You may also need double quotes in the value, so try this too:

'relation' => 'AND',
                [
                    'key' => 'wpcf-artists-medium',
                    'value' => '"wpcf-fields-checkboxes-option-ef71de9a49574f914819a39c2f7d2c95-1"',
                    'compare' => 'LIKE',
                ]

Notice that I wrapped the values in double quotes, then in single quotes.
Let's say the values is stores in a variable:

$value = 'wpcf-fields-checkboxes-option-ef71de9a49574f914819a39c2f7d2c95-1';
...
// use this
'relation' => 'AND',
                [
                    'key' => 'wpcf-artists-medium',
                    'value' => '"' . $value . '"',
                    'compare' => 'LIKE',
                ]
#2106895

Thank you Jamal, that actually worked. However, is there anything I can do when I want to check all different checkboxes? Lets say I want to verify if the user have 3 from 10 check boxes. Do I have to create a relation for each case? Or can I contain the value into an array?

I know that the question above is more my responsibility, the problem is that since the fields are coming with characters from the db .

"a:3:{s:64:\"wpcf-fields-checkboxes-option-79086d73c23771965e7bf6157fc519b0-1\";a:1:{i:0;s:7:\"Drawing\";}s:64:\"wpcf-fields-checkboxes-option-f06509a51b2f44b2f5c17d306082895d-1\";a:1:{i:0;s:9:\"Fibre Art\";}s:64:\"wpcf-fields-checkboxes-option-fa818bf226295d36c45a1f0315d7998b-1\";a:1:{i:0;s:4:\"Film\";}}"

I tried to use "LIKE" and set the value in an array but is giving weird behaviour.

#2106909

Jamal
Supporter

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

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

Well, I would say that maybe the checkboxes field is the correct field to choose. Maybe a simple Select field that accepts multiple values is more suited.

Mixing OR and AND in WP_Query and WP_Meta_Query is possible, you should be able to achieve that. Check their respective documentation:
- https://developer.wordpress.org/reference/classes/wp_query/
- https://developer.wordpress.org/reference/classes/wp_meta_query/

Check some online threads:
- https://wordpress.stackexchange.com/questions/75079/nested-meta-query-with-multiple-relation-keys
- https://stackoverflow.com/questions/24830988/nested-meta-query-with-post-where-and-multiple-relation-keys

Finally, I believe the original question of this thread has been answered. Therefore, kindly mark this ticket as resolved and open a new ticket if you have any questions about Toolset.

#2106911

My issue is resolved now. Thank you!

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