[Resolved] User View doesn’t filter by custom user field
This thread is resolved. Here is a description of the problem and solution.
Problem:
The user is using a checkboxes custom field for users.
He then created a view of users and applied a query filter on this field, but their results were wrong.
Solution:
First of all, the checkboxes field should not save 0 when an option is not selected.
Checkboxes fields are saved as a serialized array where each selected option has a unique ID and a value. Check this screenshot https://prnt.sc/131l2xn
It turns out that the user was importing the values of the checkbox from a user-made XML file instead of a Toolset Export. This file was using unique IDs, built from the same base and an incremental suffix.
This makes the view fails, because it use SQL LIKE "%option-ID%"
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.
Hi, I've added a custom field to users "wpcf-provincias-usuario", then I've built a view to display a list of user filtered by the custom field. The query filter doesn't work, I always get the complete list of users.
I tried setting a constant value or passing a shortcode attribute, it doesn't filter correctly the users.
Can you check it?
This is the view url: /wp-admin/admin.php?page=views-editor&view_id=2088
You can find the view on /usuario/ page, it is the first view on the page. You can test with 'almeria' and 'asturia' values (the last user of the list is 'asturia').
Hello and thank you for contacting the Toolset support.
View's search is not supported for users and taxonomy views, we can only filter and search within Posts view. We intend to add this feature in an upcoming release, but it will take some time before it is implemented. We have published a tutorial on how to implement search and relationship for users using a dedicated post type. Check it here https://toolset.com/course-lesson/how-to-create-custom-searches-and-relationships-for-users/
I'm filtering for the constant 'almeria', so I should get only users with that value, but I'm getting one with 'asturia' (I attach a screenshot, in my first message I detailed where you can find the view).
Later, what I want to pass is the current user cusom field values by a shortcode. They could be 'almeria,madrid,granada', will it work with more then 1 value?
Thank you! I can see the issue with the user "Juan #45".
I'll need to check at the database level. Would you allow me FTP access so I can upload the Adminer script to browse the database? Your next reply will be private to let you share credentials safely.
Otherwise, I'll need your approval to take a copy of your website and debug it locally.
I find out that the option "Asturias" has a similar ID to "Almeria" which makes it match the LIKE "%string%" SQL Query instruction. Check this screenshot hidden link
I wonder if this field is meant to hold multiple values? I mean, can a user has multiple values for this custom field? If no, I'll suggest using a Select field instead of a Checkboxes field.
Regarding the checkboxes field, I think, there is some kind of bug here, or it is probably an edge case. I'll need to check on a clean install. I'll get back to you tomorrow.
Ok, the user can select more then one value and I want to pass more then one to the filter.
I explain a little more what I need.
There are 'subscribers' and 'contributors' users who can check more then one Province. Then there are 'autors' users that works like province administrators. They also can check more then one province and list all the 'subscribers' and 'contributors' of their provinces.
You can log in to my test site with the following URL hidden link
Check the view's results here hidden link
I was able to get users that have either "Morocco" or "Japan" or both, selected in the checkboxes field.
This being said, I think that there was a glitch or a workflow issue when you were creating the values for your checkboxes field. I'll suggest that you recreate the checkboxes options again to generate random IDs for them, so they don't match an unexpected "LIKE" instruction.
Please check for the user "Juan" and the option "asturia", if it works, do the same for the other options and users. If it does not, let me take a copy of your website to check it further locally.