The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

View: Filtering based on array of user IDs


This Discussion is public



I am having a contact form having checkboxes for selecting recipients. The checkboxes are populated dynamically via php and have separate value / title values.

Now I want to create a view, which is showing only entries to the currently logged in user where the user was one of the recipients, i.e. I need a filter like "[recipient_field (array)] contains current_user". Is something like this possible without any additional php coding currently? Using "equals" or "like" does not work.

Thanks a lot,


It depends on how you've defined the check boxes and their values. Current user matches the ID of the user as WordPress defined it when the user record was created. For example, the admin user always has an ID of 1. If the separate values in your dynamically populated checkbox is using the user record IDs, a filter should work against it.

Even though check boxes are stored in the database as serialized arrays, you can't define an array as the first field in the filter. When you define a view filter, the first criteria requires you to select xa field contained in the form. You also can't use contains. While some flavors of SQL allow for a contains() function, MySQL does not. The nearest equivalent is "like". Keep in mind these filters are translated into part of the where clause in the underlying SQL statement.

If you have defined all the check boxes as part of the same check box group, then all selected values will be stored in the same serialized array. If you've never see a serialized array, here's an example of one from my dev installation. This is the data for a check box group of 2 check boxes. They have separate values of Yes and No. Their labels read Option 1 and Option 2. I selected both check boxes.


If your data is stored like this then selecting the field with like will work fine as long as you're searching for 1 value. In this case either yes or no. In your case, a single user's ID.

If your check boxes are not all in the same check box group, the filtering won't work. You would have to add each check box field to the filter, but the result will be nothing returned because the filter builder uses logical ANDs to built the SQL where clause. In other words, the SQL will read "field1 like user_id AND field2 like user_id AND field2 like user_id". This will never work because it is invalid logic. If this is how you've built the check boxes, then you have to code the where clause and use a hook.

Discussion closed.