Skip Navigation

[Resolved] Relationship Fields are not being stored as WP Custom Fields.

This support ticket is created 5 years, 3 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.

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)

This topic contains 4 replies, has 2 voices.

Last updated by Mukesh 5 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1187455

I am trying to:
I have Post Forms to create new Posts for CPT Referrals. There is another CPT called Attorneys that is a post reference to Referrals and user is selecting Attorney on the Post Form.
Link to a page where the issue can be seen:

I expected to see:
When I check teh Post Referral in the WP Admin I don't see the Attorney custom field set. Therefore I can't access it via any other views or wpv-conditional tags.
Instead, I got:

#1188109

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Mukesh,

Thank you for contacting us and I'll be happy to assist.

The post reference field forms a "One-to-Many" relationship between two post types, with an added advantage, that you can call the connected post's ID directly using the "types" field shortcode:


[types field="field-slug"][/types]

You'll replace "field-slug" with the actual slug of your post reference field.

Important note: The post which is selected in the post reference field's settings is the "parent" and the one on which field is set is the "child" in the relationship.

Apart from the "types" shortcode, you can also use a view ( ref: https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/using-post-reference-fields-to-display-information-from-a-related-post/ ) or the "toolset_get_related_posts" function ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts ) to show information from the post connected through a post reference field.

I hope this helps.

regards,
Waqar

#1190614
screenshot1.PNG
screenshot2.PNG

Hi Waqar, thanks for the info. However I'm not needing the types shortcode.

What the issue I am seeing is that when we create a Referral CPT from the frontend, the values that are picked for a Post Reference field (Attorney CPT) are not set in the WP Custom Field. If I goto the Referral post from the backend, I dont' see those post reference fields. But if I just update it from WP Admin then those fields are set. You can see by logging into my instance I gave you access.

I agree that those fields are accessible via the types shortcodes and views shortcodes but when I am using the post reference field in a [wpv-conditional] tag it is not retrieving.

I'm attaching two screenshots. Screenshot 1 is post form for Referral CPT. The field circled in blue color is the post reference field. The field circled in red is a standard field.

Screenshot 2 is the post in the wp-admin backend. as you can see the custom field for attorney is not set. So when I have a command in a view like [wpv-conditional if="( $(wpcf-attorney) eq '740' )"] is it not matching becuase wpcf-attorney is not set even though from post form I selected a value for Attorney Post reference field.

Hope that explains my situation.

#1190930

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Mukesh,

Thank for sharing further details.

I'll be happy to explain why you're experiencing this and how this can be avoided.

Since Types 3.0, post-relationship information is no longer stored in form of postmeta (custom fields) and dedicated relationship tables are used.

In some cases, post-relationship information can be accessed through custom fields, because there is a backward compatibility layer, but it has its limitations. I'll recommend going through the following resources to better understand this point:

https://toolset.com/2018/05/when-if-to-migrate-relationships-in-old-toolset-sites/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/backward-compatibility-measures-for-post-relationships/
https://toolset.com/2018/05/toolset-post-relationships-release/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

In your website's case, the attorney's information is saved with a referral post item in the relationship table, when a front end form is submitted, but you won't see a custom field entry for "wpcf-attorney" right away.

When that same referral post item will be updated through the admin area, only then you'll see a custom field entry with a key "wpcf-attorney". But again that is not its primary/intended use and it is available only as a by-product or a backward compatibility layer.

For this reason, a more reliable way of getting an attorney's ID from a referral post would be through any of the following shortcodes:


[types field="attorney"][/types]

OR

[wpv-post-id item="@attorney.parent"]

Note: Same shortcodes can also be used in conditional block comparisons:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

The expression "$(wpcf-attorney)" would only work for posts which have been added/updated through the admin area at least once, this is why I won't recommend it in your case.

regards,
Waqar

#1191306

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.