[Resolved] filter view content based on user organization
This support ticket is created 3 years, 4 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.
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.
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.
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.
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.
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:
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:
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.
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.
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.