Skip Navigation

[Gelöst] Relating post to user via author when user is not a author

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to relate a post to a user without that user being the author of the post. I would like to be able to create a View that filters based on the current User's ID, and only shows results that are related to that current User.

Solution: Add a custom field to the post type and associate a User with a post by placing the User's ID in the custom field. This can be automated using CRED or some other process if necessary. Then filter based on that custom field value. Pass the current User's ID into the View filter by utilizing a shortcode attribute. Set up your filter to compare the custom field against the User ID passed in as an attribute.

Relevant Documentation: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created vor 6 Jahre, 6 Monate. 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 6 Antworten, has 2 Stimmen.

Last updated by henrikG vor 6 Jahre, 6 Monate.

Assisted by: Christian Cox.

Author
Artikel
#573002

Hi.

This is a follow up to this post : https://toolset.com/forums/topic/make-a-custom-post-type-related-to-a-user-account/

I have created an Invoice post type, and an invoice should be related to a user account.
As you can see in the post referenced above, I decided to add a custom user-id field to the post type.

But you can't filter on user-ID's in views

I then thought I saw the solution. Since it is possible to filter posts based upon the author and set the value to the current user, I thought that I could simple change the author on the post.

But my users do not have author capabilities. So they are not displayed in the author selection list.

So I can understand that I either have to change the capabilities of my user roles, but I dont know if it is possible to change the capabilities so they can become authors only on my invoice type.

OR

I need a way to filter based upon user ID's in view, and go back to my user id field as we discussed in the previous support ticket. Right now it is not possible to filter on logged in user.

Right now I would really like to use the author option, because it is a standard wordpress feature. But my site members are not authors and should never write any posts.

So do you have any suggestions on how I could get on with this challenge 🙂

#573069

I decided to add a custom user-id field to the post type. But you can't filter on user-ID's in views
Actually you can filter by a custom field applied to the post type, even if that field represents a User ID. If I understand you correctly, you have a custom field applied to the Invoice. That custom field holds a string or number that represents a User ID. It is not the original User ID field, it's a clone. You need to filter Invoices based on this custom field, correct? This is definitely possible. The main problem is that your filter option values will include User IDs, which are unique numbers, not recognizable user names. Does that make sense? If not, what am I misunderstanding?

#573072

I have a custom post type Invoice:

This invoice is related to a user account.

The user needs a list of his/hers invoices on his / hers account page.

A filter on a view does not have the option to filter the post types based upon the current logged in user id. No matter if I have a custom field with the userid or not.

But a filter can be created that only shows the invoices based upon the current logged in author.

So I thought that I could create the invoice and then change the author to the user, but that requires the user to have author capabilities.

I think the best solution is the author since types cant relate post to users, but I do not want to give my users author rights. Only for my invoice custom post type.

#573086

This invoice is related to a user account.
Yes, you have the User ID stored in a custom field on the Invoice post, correct?

A filter on a view does not have the option to filter the post types based upon the current logged in user id. No matter if I have a custom field with the userid or not.
That's correct, there is no option for User ID, but there is an option for any custom field. You can filter Invoices based on the custom field, which is identical to the User ID. If the value of the custom field is the same as the value of the User ID, then you get the results you want, right?

#573095

Yes it is correct that I have a custom field on the custom post type for invoices where I put the user Id, to be able to relate the user id to the current logged in user.

The view should only show invoices related to the current user logged in.

The current_user_id is not a hardcoded value it depends upon the current logged in user.

It makes no sense to have a value in a field because that would not represent the current user.

The filter should basically say

if current_user_id == Value in invoice field on the custom post type.

what you are saying is

if some value in a custom field == Value in invoice field on the custom post type.

You cant take the current_user out of the equation if that is what you want to filter on.

The only way you can filter on a current logged in user in views, is by using the author filter.

The author filter is able to filter based on current logged in user, but I am unable to set my users as authors because of lacking user capabilities.

So either I need to know how to filter on a current_logged_in user id which are saved in a custom field, OR how I can give my user access to become authors, but ONLY on the invoice custom post type.

My users should not be able to write blog posts etc.

#573098
Screen Shot 2017-09-24 at 6.02.07 PM.png

It makes no sense to have a value in a field because that would not represent the current user.
You cant take the current_user out of the equation if that is what you want to filter on.
The only way you can filter on a current logged in user in views, is by using the author filter.

I think I did not explain thoroughly enough - sorry for the frustration there. Let me add some more step-by-step detail to try to clear this up.

1. You must pass the current User ID (which you can get using the wpv-user shortcode) into your View as a shortcode attribute.

[wpv-view name="your-view-slug" userid="[wpv-user field='ID']"]

2. Then, add a query filter which compares the user ID clone field against the value passed in by your shortcode attribute. See the attached screenshot showing a sample Query Filter with shortcode attribute.

Now the Query Filter compares the value of the custom field against the current user's ID.

#575486

Thanks.

Don't know why I did not think of that.

I still hope that some day I will be able to relate post types to users.

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