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.
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?
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:
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)
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.
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:
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 .
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.
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.