I am trying to: display the number of people a barn sleeps
Link to a page where the issue can be seen: hidden link
I expected to see: Sleeps: 2
Instead, I got: Sleeps:
I have 2 post types (Barns and Late Availability) which have been linked (many-to-many relationship).
The view should pull in the number of people the barn sleeps but doesn't. The field is available for me to include in the 'Loop Items' but nothing is actually displayed. My loop:
<h2>[wpv-post-title item="@late-availability-offer-barn.child"]</h2>
[wpv-post-featured-image size="large" item="@late-availability-offer-barn.child"]
[types field="start-date"][/types]
[types field="end-date"][/types]
[wpv-post-body view_template="None"]
Sleeps: [types field="sleeps"][/types]
Hi Joshua,
Thank you for contacting us and I'd be happy to assist.
In your view named "Lates", the following shortcode was being used to get the value from the "sleeps" custom field.
[types field="sleeps"][/types]
But this wasn't returning any value because this custom field value is saved with the "Barn" posts and not with the "Late Availability" posts which are being called through this view.
To get this field's value from the child "Barn" post, you'll need to introduce the "item" attribute, like this:
[types field="sleeps" item="@late-availability-offer-barn.child"][/types]
Here is a guide on using this "item" attribute:
https://toolset.com/documentation/user-guides/views/views-shortcodes/item-attribute/
Important note:
This item attribute method will only get information from one of the related posts. Since the relationship "Late Availability Barns" is set to be of type "Many-to-Many", more than one "Barn" posts can be attached to a single "Late Availability" post.
To get the list of more than one related posts, you'll need another post view, as explained at:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/#displaying-many-related-items
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Thanks Waqar,
In that case, I think I have made an error setting it up as a 'many to many' relationship. Because each offer can only be connected to one barn.
Would you advise that I change the relationship? If so, can I adjust the settings for the relationship without harming anything?
Regards,
Josh.
Thanks for writing back and yes it would be better to switch to a "one-to-many" type post-relationship in this case.
( where one barn can have many offers, but one offer can only have one barn )
Some post associations can be lost when the type of an existing post-relationship is changed. Since your website doesn't have a lot of barn and offer posts at this time, I'll recommend to delete the existing post-relationship and create a new one and make the associations again.
regards,
Waqar
My issue is resolved now.
Thanks for your further advice, Waqar. I ended up deleting the relationship because not only should it have been a 'one-to-many' relationship, but the two custom posts needed to be the other way round (swapping parent and child). After rebuilding it afresh, adjusting the Views codes where necessary , it all seems to work well. Best wishes, Josh.