Skip Navigation

[Resolved] Custom Access View

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

Last updated by Christian Cox 4 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#1230202

Hi, this is very similar to https://toolset.com/forums/topic/specialized-access-to-a-view-2/ but for some reason I cannot get this to work.

What I am trying to do is only show posts that are related to a user.

So there is a field in the post called 'Contact Owner' I want to map that field to the "user' record and match either the full name or some other key/value. So if the 'Contact Owner' = Joe Smith then only posts with contact owner of Joe Smith displays when Joe Smith is logged in.

I understand that this might not exactly be straighforward, so is there way to create a mapping table within the lookup so that it understands that contact owner is Joe Smith - then - Joe Smith = josesmith@domain.com - then - joesmith@domain.com = wordpress user 'joesmith' ... blahblah...???

I tried to use the original conditional from the other view:

[wpv-conditional if="( '[Contact Owner]' eq 'wpcf-custom-user_email' )"]

But it is definitely not complete.

Please help!

Thanks!

#1230596
Screen Shot 2019-04-14 at 10.17.32 AM.png

Okay it sounds like you have a View of some post type, and you want to filter that View by a custom field "Contact Owner". The filter should only show posts with Contact Owner mapped to the current User. You could do this with a custom field Query Filter, where the field value is set by a shortcode attribute like wpvowner. Then pass the current User's ID (or email, or whatever) into the shortcode attribute so the filter knows which posts to show.

The format looks like this:

[wpv-view name="Title of Your View of Posts" wpvowner="[wpv-current-user format='meta' info='id']"]

Example screenshot attached. This technique is called passing arguments to Views, and is explained in more detail here: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#1231206

It's a bit more compllicated than that.

I have a custom post type with a field called 'Contact Owner'. The field has the contact owner's full name (ie. Joe Smith). I need to map 'Joe Smith' to the logged in user and only display their posts. I was thinking that I could create a mapping table between the CPT and the wp_user so their is a relationship that makes sense. ie: Joe Smith = joe@email.com so that it knows that the user is joe@email.com. Is there a better way to do this?

#1231215

I need to map 'Joe Smith' to the logged in user and only display their posts
You can't do this programmatically with any level of reliability. What if more than one user is named Joe Smith? What if someone calls him Joseph Smith? What if there's a typo? You cannot use a person's name as a unique identifier, not reliably. That approach will fail eventually.

Is there a better way to do this?
Yes, my suggestion is to add another custom field that contains a User ID or User Email, and use that as the filter criteria. That probably means adjusting your workflow, Forms, etc., and retroactively changing existing posts.

#1231745

THat was my point. So how do that? How do I backwards insert/update this custom field in over 10k records?

Then, once I do that, how do I do what i Need to do with the view?

#1231872

You would use Types to create this new custom field that will hold a numeric ID equal to the User ID in wordpress. Then export your post data in XML or CSV format using WordPress's built in tools or some 3rd-party export tool. Modify that exported file by adding the appropriate User ID for each entry in the appropriate custom field. The modification process is up to you. Then re-import your site data with the new User ID field populated appropriately.

I strongly recommend you do not try this on your live site until you have run extensive testing in a staging environment, and always create a backup before importing data.

Another option is custom SQL code. You might be able to connect with an independent developer in our contractors portal: https://toolset.com/contractors

Once the data is imported you will create a View of posts and add a custom field Query Filter for the new User ID field. Set the value of the field using a shortcode attribute. This will allow you to pass the current User's ID into the filter and display the appropriate posts. We have more information about passing arguments to Views here: https://toolset.com/documentation/user-guides/passing-arguments-to-views/
And more information about getting the current User's ID available here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

#1232362

That's fine but then in the future I would need to keep doing that everytime new datra is added. It would be far easier to map this information.

I thought you could relate data using the toolset?

#1233532

That's fine but then in the future I would need to keep doing that everytime new datra is added.
In the future when new posts are added, you don't need to worry about existing posts. Simply create the new content in wp-admin or using Forms, and if you want to associate the new post with an existing User you would set a custom field value to the corresponding User's ID. It's a simple approach, but a bit tedious in that you must refer back to the User profile information to get the corresponding User ID ( the input field in wp-admin will be a simple number, not a User name ).

As far as mapping, I'm not sure I understand what you mean, exactly. We are directly relating WordPress Users and posts in this approach. The corresponding User ID is stored as a postmeta field related to the post. In wp-admin, that would be represented as a number in a custom field. Another option is to use the "post author" to connect a post and a User. This is a different type of direct relationship between Users and posts, again with no mapping required. Once the existing posts are connected to their respective Users/authors, management of new posts and post authors is straightforward in wp-admin. There is no need to remember IDs or have cross references back to the User profiles.

Another more complicated approach is to use a proxy post type that represents a User. This allows you to implement some of the functionality of a Toolset post relationship between a post and a User. One example where this comes in handy is Books and Authors. It's possible for one Author to write many Books and it's possible a Book has more than one Author. However, in standard WordPress a post can only have one author. So a many-to-many relationship between two post types is ideal. That technique is described here: https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/

#1238361

Since the 'Author' already exists and it displays the full name. I think what I should have asked is, won't that always correctly map to a user automatically when they login using their SSO credentials. For example, if 'Joe Smith' exists and then Joe logs in with his email address (joesmith@....), it will map to Joe Smith's posts since the same name exists on his User Profile. Or did I miss something?

If I'm right then all we would need to do is only display posts by author?

#1238363

if 'Joe Smith' exists and then Joe logs in with his email address (joesmith@....), it will map to Joe Smith's posts since the same name exists on his User Profile
If Joe Smith logs in, and the same Joe Smith User account has created posts, then those posts are inherently associated with Joe Smith's User account as the post author. If that's what you mean by mapping, then yes.

You can then filter a View of posts by post author in several ways, as described here: https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

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