I have two custom post types, Companies and Sites, Sites is a one to many child relationship of Companies i.e. a company can have multiple sites. I have created a view called site list as shown in screenshot 1. I want to be be able to filter this view based on the logged in user , where by the user drives what Companies can be seen.
For Example (using screenshot 1 example)
UserA - Role = Dingbro Ltd, they should only see Dingbro company sites.
UserB - Role = Safe Building Services, they should only see Safe Building Services Sites.
As per screenshot 2, I can filter the list manually using the filter by post relationship filter and selecting the company, so thought I may be able to use this to dynamically select the company name from the user role field (this stores the company name).
After hours of trying and reading support posts I though I maybe able to use the shortcode [wpv-current-user info="role"] however if I put this into the filter "Post with ID set by shortcode attribute" section of the filter it wont accept it as it says shortcode "Only lowercase letters and numbers allowed as shortcode attributes" as per screenshot 3.
I am a newbie to toolsets and PHP I am learning quite slowly so any help greatly appreciated.
Unfortunately you wont be able to filter the view like this when its in the block editor.
What you need to do is to add the query filter to filter the view by the shortcode attribute and give that attribute a name.
Then you need to re-add your view by using Classic block where it will add the shortcode of the view.
The end result should be
[wpv-view name='my-view' my-attribute='[someshortcode]']
Where my-view is the slug/name of your view , my-attribute is the attribute that you've created in the filter and [someshortcode] is the value that you are passing into the view.
I am getting there and worked out how to use the view shortcode but I'm still struggling with the filter element.
I have created [wpv-view name="site-list-new" @company.parent=[types usermeta='comp' current_user='true']'] but its not filtering to only include the usermeta comp name I can see all companies in the list.
If I use the shortcode [types usermeta='comp' current_user='true'] on the page on its own I get the desired result i.e Dingbro Ltd for the current logged in user. Which matches the Company Parent name that I am trying to filter it on, so I assumed this would remove the Safe Building Services ones from the view but it doesn't remove any.
Thanks for your help, it's steep learning curve but I'm getting there slowly.
I have created [wpv-view name="site-list-new" @company.parent=[types usermeta='comp' current_user='true']'] but its not filtering to only include the usermeta comp name I can see all companies in the list.
I see the issue here. I'm assuming that you've created the shortcode attribute for the filter and called it @company.parent, however this isn't an allowable namespace.
Try using 'company_parent' then you will pass the parameter into the view like this.