Skip Navigation

[Resolved] How to filter user Views with user roles?

This support ticket is created 5 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by Pat 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1238190

Pat

Hello,

I'm working on a site with lots of users. I have created a front end page in order the site manager to be able to create / modify or suppress user, without having to access the back end.
In a first step, I have listed all users, but I need to offer the ability to sort in the front end thanks to the following parameters :
1. user role
2. user name (text search)
3. user email
4. user meta field

Is there a way to have these filters available and how ?
Regards
Par

#1238194

Hi, there's not a built-in way to add parametric searches to User Views. Instead, you can use a proxy post type that holds all the same information. We have some information about this process available here: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/

#1238347

Pat

Hi Christian,
Thanks for taking care.
I was more in favor of building a kind of url parameter in order to add it to the page where the Views is.
For example, let say I add filtering options (url parameters) in my Views for :
role
name
Then, I could use an url like : hidden link

Saying this, I'm getting 2 issues :
1. when speaking of role, the information is available in the usermeta wp_capabilities. That's fine but then, the field content in the database is something like a:1:{s:6:"editor";b:1;} So, I'm not sure how to render this starting from the role name (here editor).

2. next is to have the right tools to define the parameter string to add to the page where the Views is. Do you have any recommandation for this. Here, I need to have the ability to chose the role in a list of roles, place the name inside a text field and so on for the other conditions. When everything is defined, then validate and launch the new url in order to display the result.

Any idea is welcome !
Regards
Pat

#1238350

I know there are a couple of APIs available for filtering User queries:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_post_query

If the View uses pagination or table sorting you can add your own custom code, like custom filters, in the Search and Pagination editor of the View. You can use wpv_filter_user_query to modify the query parameters as described in the WP_User_Query documentation:
https://codex.wordpress.org/Class_Reference/WP_User_Query

That's about all we offer in terms of custom User searches.

#1238382

Pat

Hi Christian,

Sounds that these API should make the work !
Now, I'm a little bit unsure how to use them.
How to create the parameter search form to place in the same page than the Views and how to link it to the right API?
Could you explain a little please?
Regards
Pat

#1238654

The search form is outside the scope of what I can support here, unfortunately, because there is no JavaScript API for search form filters, and we do not assist with filter customizations. Also, it is explicitly stated in our documentation that you cannot create parametric search forms for Users with Views. You're on your own when it comes to developing custom filters, and integrating those with filter queries. My suggestions are:
1. Do not use AJAX, and do not attempt to update the filter options based on other filter selections. There are no public APIs to facilitate those features.
2. The selected options may be in one of the superglobals like $_GET or $_POST, so you can inspect those in your API callback for more information.

To filter a User View using the wpv_filter_user_query API, you add the API hook and callback to your functions.php file or a new custom code snippet, like you would add a CRED API hook or a custom shortcode. You can use the $view_id attribute to make the filter conditional based on the View ID.

#1238787

Pat

Hi Christian,

I can understand your position.
Nevertheless, I was looking at a different solution :

We know that the url parameter works fine (www.my_site.com/page_of_the_views?nam=xxxx&role=yyy). So, wouldn't it be possible to create a Cred in order to define the filters and then, thanks to Cred_save_data or something else, redirect to the requested page (ie : ).

With this, it would be easy to define all different filters needed thanks to generic cred fields for example.

Is there a way to redirect to a specific page (defined by the Cred) after the Cred validation?
Regards
Pat

#1238800

So, wouldn't it be possible to create a Cred in order to define the filters and then, thanks to Cred_save_data or something else, redirect to the requested page (ie : ).
Is it possible to use CRED to create a custom search View's filters? No, that is not possible. You should not place a CRED Form in a custom search View's filter area.

Is it possible to use CRED to create a post, and use the cred_save_data and the cred_success_redirect API to redirect to another page or URL after submission? Yes, you can see more about the redirection API here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
Here's the save data API:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Is there a way to redirect to a specific page (defined by the Cred) after the Cred validation?
The cred_success_redirect hook is fired after the form is submitted successfully. The cred_form_validate API just returns to the same page if there is an error, but there is no custom redirection available in that API.

#1238827

Pat

Hi Christian,

Thanks for your quick answer.
Not sure to have been sufficiently clear.

Currently, I have a page (www.my_site.com/page_of_the_views) with a Views that list users (including some filters thanks to url parameters).

I want to place a Cred in top of this page in order to make the selection (could be by a textfield to retrieve a specific user, or number for user ID ...).

The aim is after the Cred validation, to redirect to an url like this :
hidden link"toto" or hidden link

Doing this, the Views will use the url parameters and list only the selected users. I can understand this is not the best way to manage it but to my sense, this could work knowing what my customer wants.

So, I will try to use the cred_success_redirect hook for that.
Do you have advice on how to pass data from the Cred to this form (in the hook, I will need to retrieve the parameter values that have been defined inside the Cred to use them to create the redirection url).

Thanks again for your patience.
Regards
Pat

#1238842

All the input fields in the Form will be available in the $_POST superglobal. If you want to inspect those in the logs, you can do something like this:

add_filter('cred_success_redirect', 'custom_redirect_from_editor',10,3);
function custom_redirect_from_editor($url, $post_id, $form_data)
{
  error_log(print_r($_POST, true));
  return $url;
}

Example output from one of my Form submissions:

[02-May-2019 20:38:46 UTC] Array
(
    [book-tax] => Array
        (
            [0] => 27
        )

    [book-tax_hierarchy] =>
    [new_tax_text_book-tax] =>
    [new_tax_select_book-tax] => -1
    [wpcf-book-sel-1] => 2
    [wpcf-book-cbs-1] => Array
        (
            [0] => wpcf-fields-checkboxes-option-6b18daf0572facfbde18e9d74e08d1a4-1
            [1] => wpcf-fields-checkboxes-option-36aed90534c581f0378c7e780fba166f-1
        )

    [wpcf-book-cb-1] => 1
    [_wptoolset_checkbox] => Array
        (
            [cred_form_22_1_wpcf-book-cb-1] => 1
        )

    [wpcf-book-date-1] => Array
        (
            [display-only] => May 3, 2019
            [datepicker] => 1556841600
            [datetime] => 2019-05-03
            [hour] => 00
            [minute] => 00
            [timestamp] => 1556841600
        )

    [wpcf-book-email-1] =>
    [wpcf-book-num-1] => 12
    [wpcf-book-rept-img-1] => Array
        (
            [0] => <em><u>hidden link</u></em>
            [1] => <em><u>hidden link</u></em>
        )

    [form_submit_1] => Submit
    [_cred_cred_wpnonce_cred_form_22] => 01e012c030
    [_cred_cred_prefix_post_id] => 105
    [_cred_cred_prefix_cred_container_id] => 23
    [_cred_cred_prefix_form_id] => 22
    [_cred_cred_prefix_form_count] => 1
)

Remember, the Form must be set to redirect to some Page or Post in order for this code to be triggered.

#1238946

Pat

Hi Christian,

Sounds good !
Concerning the form parameter, if I understand well, I can chose any of the available pages in a first step and the hook will redirect to the chosen url whatever the chosen page?

Second : is it possible to restrict this hook to only one Cred (I don't see the $form_id in the list of parameter passed)

A last point : as I do not need to record the results of the Cred in a postype, is there a way to avoid that, or at least, to use always the same post ID to record it?
Idea here is not to make database grow with things that are not necessary !

Thanks
Pat

#1239500

Concerning the form parameter, if I understand well, I can chose any of the available pages in a first step and the hook will redirect to the chosen url whatever the chosen page?
Correct, the hook may override the page you choose in wp-admin redirect settings, optionally. If you do not modify the URL in the hook, the page you chose in wp-admin will be used.

Second : is it possible to restrict this hook to only one Cred (I don't see the $form_id in the list of parameter passed)
Yes, form_id is an attribute of the $form_data parameter, which is an array. It's best to use the Form ID from $form_data, not from $_POST. Refer to the documentation and example here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

A last point : as I do not need to record the results of the Cred in a postype, is there a way to avoid that, or at least, to use always the same post ID to record it?
I guess you could create one post and edit that same post every time someone submits the Form.

#1240787

Pat

Hi Christian,

I'm OK with all you proposed.
Many thanks
Pat