Skip Navigation

[Resolved] filter view content based on user organization

This support ticket is created 2 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 13 replies, has 3 voices.

Last updated by salimA 2 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2127597
users.JPG

Dears,

I am trying to filter the users based on the user registered organization. I have did based on your suggestion on the below links.

https://toolset.com/forums/topic/filter-view-content-based-on-user-organization/
https://toolset.com/forums/topic/link-university-admins-and-users/

both the ways worked at the beginning then after some days it stopped.
Now, all user's registration showing on all pages. Check the attached image.

#2127991

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

HI Salim,

Thank you for getting in touch.

Would you mind allowing me to have admin access to the site so that I can check on this for you in a bit more details ?
Please send me a link to the page that you're having the issue with.

Thanks,
Shane

#2128957

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Salim,

I believe I found the issue.

If you look at the field group below you will notice that the ORG field doesn't have any options to select from.
hidden link

Perhaps re-add your options here.

Thanks,
Shane

#2130541

As per Waqar's suggestion in the above ticket. I have created a post type for universities and I added them there. then I use the below code to read them and retrieve them in the select field.

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpt_field_options', 'populate_university_field_options', 10, 3);
function populate_university_field_options( $options, $title, $type ){
switch( $title ){
case 'Organization':
$options = array();
$args = array(
'post_type' => 'university',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
$options[] = array('#value' => '', '#title' => '---',);
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}

#2131935

Hello, Shane is on holiday this week and I'm checking in on his open tickets. I was trying to log into your site to see what the problem could be, but I kept getting timeouts today when I tried to access wp-admin. I will try again tomorrow and give you another update as soon as I can.

#2133027
Screen Shot 2021-08-03 at 8.28.30 PM.png

Sorry but I still can't access the site today. Here is a screenshot of the message I see when I try to access /sarhoman or /sarh-users/

Are you able to access the site? Is it possible there is a location-based IP restriction or some other reason I could not visit the site from USA?

#2133181

Hi Christian,

Strange, Shane is able to access the website from his side. and it is working fine from my side.
You may need to add the IP in the host file of your device.

#2134023

Okay, what should I add?

#2134309

Please open a private ticket to share the details ..

#2134799

I have activated private fields here. Add any dummy information necessary to submit the private reply.

#2140613

Got it, thanks. It turns out I had a different IP address in my hosts file already. I must have worked on this site in the past, and forgot about the IP in my hosts file.

the issue is when you log in with each above account the same data of both organizations showed.
I was able to log in using all 3 accounts. I corrected some problems. First, the Types User field shortcode was not working well. The syntax was something like this:

[types usermeta='org' current_user='true'][/types]

The correct syntax should be user_current, not current_user...but this still did not solve the problem for me. I placed this shortcode in the page contents as a test, and the org field was always showing up as empty. I replaced the Types shortcode with a similar Views shortcode, and now the results seem to be filtered by org. Now the View shortcodes look like this:

{!{wpv-view name="pending-users" org="{!{wpv-user field='wpcf-org'}!}"}!}

{!{wpv-view name="create-users-accounts" org="{!{wpv-user field='wpcf-org'}!}"}!}

{!{wpv-view name="all-users" org="{!{wpv-user field='wpcf-org'}!}"}!}

When I log in as admin, I see GuTech org items. When I log in as mctuni, I see Muscat Uni items.
I edited the ralrashdi User and selected the default Organization "---" temporarily. If the default "---" Organization is selected, the User will see all items, unfiltered by org. If that is not what you expect, please tell me more about what you want when no Organization is selected. Let me know if the results are correct now. I can continue to investigate why the Types usermeta shortcode is not working correctly in this page.

#2140851

Dear Christian,

Thank you very much for your assistant. Yes, the filter now works fine.

about if the user does not select any organization, this is a good point, I did not think about it. The system should not allow the user to submit without selecting any organization. when I did the org field (required), the org items hide from the form because it comes from the post type.

Any suggestions to solve this.

#2141111

The system should not allow the user to submit without selecting any organization. when I did the org field (required), the org items hide from the form because it comes from the post type.
If Users register using Toolset Forms, you can implement our Forms Validation API cred_form_validate to show an error if the Form submitter does not select an Organization. For example:

add_filter('cred_form_validate','tssupp_org_validation',10,2);
function tssupp_org_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //validate if specific form
    $forms = array(1234, 5678);
    if (in_array($form_data['id'], $forms))
    {
        //check org value
        if (!isset($fields['wpcf-org']['value']) || $fields['wpcf-org']['value'] == '')
        {
            //set error message for my_field
            $errors['org']='You must select an Organization';
        }
    }
    //return result
    return array($fields,$errors);
}

This code will validate the org field selection in Forms with IDs 1234 and 5678. You should replace 1234, 5678 with a comma-separated list of the Registration Form IDs you wish to validate. Place this code in your child theme's functions.php file or in a new custom code snippet in Toolset > Settings > Custom Code.

#2142901

Hi Christian,

Thank you for your support, the code is working and the form is working as expected now.
The issues are resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.