I have a site with TWO custom post types.
Clients
Vehicles
A client can own many vehicles but a vehicle can only be owned by one client.
On the Vehicle post template I want to be able to show information about the Client which owns the Vehicle
e.g. the Client's address, phone number, first and last name.
The Client post type has a field "ClientID"
In the Vehicle post type, "ClientID" is a Post Reference post type i.e. when the admin enters a new Vehicle into the database they need to assign that Vehicle to a ClientID.
Can I use the Post Reference post type as the "connector" between the two post types in order to get the Client address, phone number, first and last name? How?
OR is it really necessary to use a Post Relationship?
When is it better to use a Post Reference vs a Post Relationship?
Whats the differences?
Can I use the Post Reference post type as the "connector" between the two post types in order to get the Client address, phone number, first and last name? How?
Yes, you can use the Post Reference field to access information from a related post. The method you use depends on whether you are using the Block Editor or the legacy editor to design the Vehicle post template. If you are using the Block Editor, you can insert a Fields and Text block in the Vehicles template, then click the icon above the block to insert Fields and Views, and choose one of the Client fields you want to display. See fields-and-text.png for an example.
In the legacy Editor, you would click the Fields and Views button and choose one of the Client fields.
After you have opened the Fields and Views popup in either the legacy editor or the Fields and Text block, click the Post Selection tab, where you will be able to choose "a related post from a post reference field", and choose the Client post reference field. See post-selection.png for an example.
When is it better to use a Post Reference vs a Post Relationship?
Whats the differences?
A post reference field is a very simple version of a one-to-many (O2M) relationship. In the database, they are handled identically. The main differences are how they are managed in wp-admin. For example, with a O2M relationship between Clients and Vehicles, you can edit the parent Client post and see all the child Vehicle posts easily in the post relationship editor panel. If you want to disconnect one of the Vehicle posts, you can do it from the Client post. However, with a post reference field, you cannot easily manage the related Vehicles from the Client post. You would have to edit each Vehicle post to change the post reference Client. So if you want more management capabilities in wp-admin, you should use a O2M relationship. If you do not need this type of management, you can use a Post Reference field instead.
If you need the ability to create multiple Client post references in the same Vehicle post, you should use a M2M relationship instead of a repeating post reference field.
OK. Thank you.
Thats a bit clearer now.
If understand correctly:
Post References are uni-directional
Post Relationships are bi-directional
Meaning that, if I use a Post REFERENCE FIELD in the Vehicle post type to attach it to a Client:
- The Vehicle post can pull custom fields from the Client post
BUT
- The Client post CANNOT pull custom fields from the attached Vehicle posts
Correct?
Whereas, if I use a Post RELATIONSHIP between Vehicle post type and Client post type
- The Vehicle post can pull custom fields from the Client post
AND
- The Client post CAN pull custom fields from the Vehicle post
Correct?
Post References are uni-directional
Post Relationships are bi-directional
Meaning that, if I use a Post REFERENCE FIELD in the Vehicle post type to attach it to a Client:
- The Vehicle post can pull custom fields from the Client post
BUT
- The Client post CANNOT pull custom fields from the attached Vehicle posts
Actually no, this is not exactly true. You can use a View of Vehicle posts with a post relationship filter, set by the reference field, to display information about all the related Vehicle posts in the context of a Client post. The only significant difference between the reference field and an O2M relationship is in the wp-admin management of the referenced/related posts. The data available from either side of the reference field, on the front-end, is no different from a O2M relationship. A View is required in either case to display that Vehicle information, because multiple Vehicles can be related to the same Client post.
Thank you. That was a great explanation.
You can close the ticket now.