Skip Navigation

[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 3 years, 6 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 15 replies, has 2 voices.

Last updated by Jamal 3 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#2057715

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').

thanks

#2058031

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/

On the other hand, you can still hook into the user's view query and modify it to introduce a search parameter. Check the wpv_filter_user_query filter here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
And check the argument that can be passed to the underlying WP_User_Query instance here https://developer.wordpress.org/reference/classes/wp_user_query/#parameters

Check an example of using the hook on this ticket https://toolset.com/forums/topic/need-to-query-a-user-view-based-on-email/

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

#2058221
Captura de pantalla 2021-05-17 142016.png

Hey, I don't want to add search filters, I've just added a Query filter (check the image).

Shouldn't it work?

#2058315

The query filter should work and the view should return only the users that match the query filter.

Can you provide an example of a user that appears in the view's results when it shouldn't? I'll try to debug it from there.

#2058325
Captura de pantalla 2021-05-17 152626.png

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?

cheers

#2058355

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.

#2058475

Hi Jamal, it seems the private message is not active.

#2058535

Hey, it seems it is not private this message. Please, check it.

#2058541

Hey, it seems that this message is not private.

#2058607

My apologies. Now, it would be private.

#2058637
#2058649

Here it is

#2058669

Thank you!

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.

#2058719

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.

cheers

#2059549

I run a test in a clean install and I was able to have a view that takes values in a shortcode argument, separated by comma, like:

[wpv-view name="users-by-country-as-a-shortcode-argument" country="Morocco, Japan"]

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.