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 🙂
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?
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.
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?
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.
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.
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.