Skip Navigation

[Resolved] I cannot get custom fields from related posts in a view

This support ticket is created 4 years, 9 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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/Karachi (GMT+05:00)

Author
Posts
#1513385

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]

#1514661

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

#1514821

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.

#1516277

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

#1516515

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.