Skip Navigation

[Resolved] How to use Post Reference fields and/or Post Relationships, for required fields

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am trying to understand how and when to use Post Reference fields and/or Post Relationships. See details here:

https://toolset.com/forums/topic/how-to-use-post-reference-fields-and-or-post-relationships-for-required-fields/#post-1124617

Solution:

The "post reference field" is similar as one-to-many relationship, it will create another independent one-to-many relationship, see details here:

https://toolset.com/forums/topic/how-to-use-post-reference-fields-and-or-post-relationships-for-required-fields/#post-1125195

Relevant Documentation:

https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-one-related-item-parent

This support ticket is created 6 years, 1 month ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by johnnyH-3 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1124617
Database for ultra running results.png

I am trying to understand how and when to use Post Reference fields and/or Post Relationships.

Setup
Let's take two CPT examples, EVENTS and RACES.
1. I have set up a one-to-many relationship.
2. I ALSO have a Post Reference field in RACES, where I pick the one event that it is related to (this is a required field).
3. I do not show the relationship box for the EVENTS << RACES relationship in the RACES post editor (as I am using the Post Reference field instead).

Results
1. I can add a new race to an Event in the relationship box. But if I then go to that race, there is no selection of that event in the post reference field.
2. When I view an event, I cannot see the races that are supposed to be related via the Post Reference field (that is, when I have selected an event in the Post Reference field of a race). The list of related races for that event is empty.
3. If I connect an existing race to an event, in the relationship box below the custom fields, the ONLY option I get is the race from which I already have selected that event in the Post Reference field.
4. I have a view that is supposed to show all upcoming races, sorted by the race_startdate field in RACES, but with the title and location from its parent event. I have the content types EVENTS and RACES selected, a query with "In any relationship" related to "The current post in the loop", that is supposed to output [wpv-post-title item="@race_event.parent"] (the title of EVENTS), [types field='race_startdate'] (the startdate from RACES) etc. But at the moment it only outputs one event/race, even though also other events both has a race as a relation, and the same event referenced from a post reference field in RACES. (I have not yet added a query filter to only display races with dates in the future, as to avoid the risk of this query hiding some posts at this stage).

Obviously I can't simultaneously use Post Reference fields and Post Relationships this way. Or do I always have to BOTH add the relationship AND the post reference field? The fields where I am using Post Reference are required. There cannot be a race that is not related/referenced to an event. With only a Post Relationship I can add a race that has no event... which should not be possible.

I'd love some guidance on how to set these relationships up, in order to also display views with fields from multiple CPT's. Perhaps I should not use Post Reference fields at all?

Attached is the entire database model for reference. Thanks!!!

#1125195
races.JPG

Hello,

Thanks for the details, I can login your website.

I assume we are talking about the custom relationship field "Evenemang":
hidden link

And the post type relationship "Evenemangets tävlingar":
hidden link

As I mentioned in your previous thread:
https://toolset.com/forums/topic/help-with-database-model-for-relationships-and-possible-intermediary-post-type/
The "post reference field" is similar as one-to-many relationship, it will create another independent one-to-many relationship between post type "Events" and "Races", and it display only when you edit a single child "Race" post, for example:
hidden link
field "Evenemang*"

Since it works like a custom field, so it does not display when you edit a single parent "Event" post, for example:
hidden link
There isn't field "Evenemang*"

And for the questions:
do I always have to BOTH add the relationship AND the post reference field?
I suggest you choose only one of them ("post reference field" or "Post type relationship"), for example, when you edit a single parent "Event" post, if you don't need to setup it's child "Races" posts, then choose "post reference field", otherwise chose "Post type relationship".

For example, if you choose "Post type relationship" optin, and use the post type relationship "Evenemangets tävlingar", in a single "Event" post, you can display the related "Race" posts by a post view "Lista tävlingar för evenemang 2":
hidden link
query posts of post type "Race", filter by:
- Select posts in a Evenemangets tävlingar relationship that are related to the Post where this View is shown.
- Select items with field:
Startdatum is a number greater than or equal TODAY()

In the loop, display the "Race" information

Put above post view into a single "Event" post content
hidden link:

<hr />Related Race posts:
[wpv-view name="lista-tavlingar-for-evenemang-2"]
<hr />

See the result here:
hidden link
See screenshot races.JPG

It works fine, and display the related two "Race" posts correctly, please test it.

And the your previous post view "Lista tävlingar för evenemang" does not work as expected, I suggest you use the new post view "Lista tävlingar för evenemang 2" to replace it. If you need assistance for it, new ticket please.

#1125264

Thanks, now I understand better when to use Post Reference or Post Relationship. Basically only Post Relationships in my case, even if that may cause a Race to not have an Event related to it.

The view "Lista tävlingar för evenemang 2" does work, kind of... But in the view template I am also trying to display fields from the Courses post type, which is also related to Events (as well as to Races with a one-to-many relationship). But I can't get fields from Courses to display. Been trying a few different options... If I get that to display correctly, I think I'm on my way to understand most of what I need for now. 😀

#1125856

I assume we are talking about the post view "Banor för ett evenemang":
hidden link

I have done below modifications in your website
1) Edit above post view "Banor för ett evenemang",
in section "Content Selection", choose post type "Banor"

2) in section "Query Filter", change the filter to:
Select posts in a Evenemangets banor relationship that are related to the Post where this View is shown.

3) in section "Loop item in Banor för en tävling", add a post link shortcode:
[wpv-post-link]

4) Put above view in layout "Mall för Evenemang":
hidden link
just below the text "Tidigare tävlingar", since your website isn't in English, I am not sure where should put it.

Test the event post here:
hidden link

It works fine, for those custom field shortcode in view's loop, you will need to check the field slug, make sure those fields are setup in the single "Course" post.

#1125905

No, in the view "Lista tävlingar för evenemang 2" I want to include the field "Distans" (course_distance) from the post type "Banor" (Courses).

I have the field in the view template for the view "Lista tävlingar för evenemang 2". But it does not display on hidden link. So obviously it has to be added in a different way.

#1125932

The post view "Lista tävlingar för evenemang 2" is querying posts of post type "Tävlingar", you are going to display field "Distans" of other post type "Banor", but there isn't any direct relationship between "Tävlingar" and "Banor", see your post type structure:
hidden link
so you can not display them directly.

In your case, you can setup one-to-one relationship between post types "Tävlingar" and "Banor", edit each "Tävlingar" post, connect it with another "Banor" post, then in post view "Lista tävlingar för evenemang 2" loop, you will be able to display the related "Banor" information and field "Distans", see our document:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-one-related-item-parent

#1125935
Database for ultra running results.png

Thanks. I actually have set up a one-to-many relationship between Banor and Tävlingar (as each course in Banor can be in many Tävlingar). I will read on that link and try again and report back here.

Attached is an updated data model that I'm working from now.

#1125946

Hmm, still can't get it to work... The following field does not display in the view "Lista tävlingar för evenemang 2":

[types field='course_distance' format='FIELD_VALUE' item='@tavling-bana.parent'][/types]

But the field course_distance belongs to the parent post type of the "Tävlingar" post type as defined in the relationship tavling-bana (Tävlingar som använder banan"? So why does it not display?

The same method does work to include the wpv-post-title from another parent post type, as defined in the relationship evenemang-tavling...

#1125949

Actually, I did find the cause. When I had made some changes in the post type fields the value for course_distance had disappeared. I edited the posts and the fields now appear as I wish. Thanks for your help and patience!