Hi. I'm developing a site where a user will have a login (based upon their email address) and an a client id (in a custom field - 'client_id'). When they try to view this particular view (or any associated view) I only want them to see posts (they are from custom post type) related to their 'client_id'. If there are no items related to them then I want to display a message (ie. "sorry, no orders to show').
The logged in user will be a subscriber and I'm using Access.
They will go to the first view template which is a listing of orders. Then they will have the opportunity to click on one of them and see more details (a view archive).
I understand I need to use a conditional but I am unsure, based upon the documentation how to make this work. Can you please help? Thanks!
Hello,
How do you setup the "client_id" field?
Is it a custom user field created with Types plugin?
How do you relate the "client_id" field with posts?
Is the "client_id" field's value correspond to post's author?
If it is, you can get current login user's "client_id" field value with Types shortcode:
[types field="client_id" user_current="true"][/types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#textfield
user_current:
"true" - output user field for the logged in user
with Types shortcode
Then pass it to below post view:
- create a post view
- query posts,
- filter by
Post author is set by a Views shortcode attribute "author_id"
And display above post view, like this:
[wpv-view name=”View Name” author_id='[types field="client_id" user_current="true"][/types]']
More help:
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/
Section "Post author is set by a Views shortcode attribute"
It was not setup with Types.
Which one is not setup by Types plugin? please elaborate the questions with more details.
1) How do you setup the "client_id" field?
2) How do you relate the "client_id" field with posts?
1 - Nothing was setup with Types. These are custom Post Types. I just happened to be using the Toolset to create the Views.
2 - The ClientID is a custom field in the post. So I don't need to relate it. It's already there.
If they are standard WordPress custom user field, you can use Views shortcode [wpv-user] to get the field value, for example:
[wpv-user field="wpcf-custom-user-field"]
See our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
If the shortcode is inside a Views user query, it will display information for the user found in loop. Otherwise it will display information for the current logged-in user or the user with the specific id, when id attribute is set.
So I can use this:
[wpv-user field="wpcf-custom-user-client_id"] (thanks!)
but how can i insert a message for a user who has no results?
Larry
Dear Larry,
If there isn't any result found in a post view, in section "Loop Editor", within shortcode [wpv-no-items-found]...[/wpv-no-items-found], you can display the message as you want.
See our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-no-items-found
Content wrapped with [wpv-no-items-found][/wpv-no-items-found] will be output if there are no posts, taxonomies or users returned by the View.
My issue is resolved now. Thank you!