How to select one or multiple users as (co-)authors of a custom post type post
- This topic has 3 replies, 2 voices, and was last updated 2 months ago by
Peter.
-
AuthorPosts
-
February 10, 2025 at 11:30 PM #47635
John Connor
ParticipantHi! I'd like to know how to best use Meta Box AIO to display one or more users as authors of a custom post type.
In my use case scenario I made a custom post type, called 'movie review'. Sometimes only one user will be the author of a movie review, but sometimes multiple users will be working on the review and they must all be credited for having co-written the review.
What is the work flow for this scenario? How to display it on the front-end? Correct display on the front-end is tricky because if only one author wrote the review, on the front it should say: "Author: " and then the user name. But when there would be multiple authors, it should say: "Authors: ".
I should be able to select from all users that are registered on my site somehow.
Thanks!February 12, 2025 at 9:49 PM #47642Peter
ModeratorHello John,
You can use the
user
field or create a relationship between users and the post type to select more users per post. Then follow the documentation to output the user in the frontend with code.
https://docs.metabox.io/fields/user/
https://docs.metabox.io/extensions/mb-relationships/February 13, 2025 at 6:27 PM #47648John Connor
ParticipantThanks Peter, I read those pages now. I have trouble understanding why someone would use the one over the other option. Are there advantages and disadvantages of using the user field versus setting up relationships? In what cases is it smarter to use the one over the other? If it helps: I would want users to click on the names, and then be redirected to more reviews written by the author whose name was clicked on. Perhaps that helps.
Then this: If I use the User field option, where would I put the code snippets provided under the heading 'Template usage' (found here: https://docs.metabox.io/fields/user/#template-usage) to display the authors of the review on the front-end? In my functions.php or in the MBViews screen?
The MB Relationships option is even more challenging for a beginner like me: how would I even display the users (authors) who I connected to the post type (review)?
Also: how to make this display conditional? As an example, if the review has only one author I would like to display only that name on the front-end, e.g. 'Caroline Stone'.
But if a review would have two authors, I would like to add the word 'and' between the two names, e.g. 'Caroline Stone and Brodie Hess'.
Lastly, if a review would have three or more authors, I would like to add a coma between each author, and then the word 'and' between the second-last and last author name, e.g. 'Caroline Stone, Brodie Hess, and William Cain'.I don't know how to do this sort of conditional displaying on the front-end with the user/mb relationships ruote. I hope you can help guide me a bit here.
Thanks so much!
February 14, 2025 at 9:57 PM #47662Peter
ModeratorHello,
There is no case smarter than another case. It just depends on your requirements when setting up your project
-user
field will be saved to the default WordPress tablewp_postmeta
and works like a normal custom field.
-relationship
data will be saved to a custom tablewp_mb_relationships
. You need to use a custom query to get the data.
They are already noted in the documentation that I shared above.To output the data, you can add the code to the template file in the theme folder or use MB Views. For the advanced, you need to have knowledge about coding to create the code.
-
AuthorPosts
- You must be logged in to reply to this topic.