Skip Navigation

[Resolved] Upgraded to Toolset 2.3, need to convert legacy code

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

This support ticket is created 6 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by John 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1079724

My site currently makes use of the code " _wpcf_belongs_post_id". I just upgraded to Types 3.0.7 (but haven't yet migrated to the new post relationships). My understanding is that this code no longer works with this Types upgrade and needs to be changed.

I'm wondering which of the shortcodes at the link below I should use—and how—to replace the above legacy code/field.

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Specifically, I have been using this shortcode in a Gravity Forms merge tag.
1. Using the Form, a user creates a new custom post type and selects a city from a dropdown menu in the Gravity Form (City is the parent post type for the new post).
2. The value selected in that dropdown is then added through some Gravity Forms magic to the _wpcf_belongs_post_id field for the new post.
3. This sets the relationship in Types.

Is there a new custom field/database field that I should use? Or is more required to get this working again?

#1080203

The codes at the link are API Functions, not ShortCodes.
All ShortCodes that used the hidden parent field as you show it should proceed working even if you do not replace them
The new ShortCodes for relationships is automatically inserted by the GUI when you insert it fresh, so if you would want to replace such codes you would need to use the GUI.

However, if you plan to not use those ShortCodes on NEW relationships you use then you do not need to touch it.
Migrations that are older are flagged, and stay compatible.

If you instead used Custom PHP functions to get related posts by that hidden parent field, you may refer to the link you shared (here below again):
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
That API will allow you to get related posts of old or new relationships.
Usually, I'd refer to this:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
It gets you all related posts of a relationship to a given parent, for example.

To know exactly what code you'd need I'd need to see your code.

>2. The value selected in that dropdown is then added through some Gravity Forms magic to the _wpcf_belongs_post_id field for the new post.

The magic that Gravity Form does, in this case, is adding the post_meta with key _wpcf_belongs_post_id and value of parent post id to the post you edit.
That will fail with new Relationships, but not with old or migrated relationships.

With new relationships, you'd need to use https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts.
This does not require anymore any code that updates any meta of a post.

Instead, you need to replace the magic of Gravity forms that updates the post meta (probably update_post_meta()) and use something like toolset_connect_posts( 'posttypea-posttypeb', 5, 7 );, where you will change the 5, 7 to the ID's of posts you want to connect.
posttypea-posttypeb is the slug of your relationship or array of post types if it's a legacy relationship
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#1081647
Screen Shot 2018-08-13 at 2.49.47 PM.png

Thanks Beda. After doing the full migration to the new relationships system, everything displays on the front end properly.

However, there's still the issue with Gravity Forms and creating the post relationship when the form is submitted. In my Gravity Form, I use the merge tag/field, as shown in the attached screen shot. Previously, whatever city was selected in another dropdown on this form would then be merged to this City ID field, which would then create the relationship automatically when the form was submitted (by writing the ID to the "_wpcf_belongs_city_id" field.

-First, to confirm: are you saying that there's no longer any field to write the value (City ID) to?

-Or Is the hidden field "_wpcf_belongs_city_id" replaced with a different field in the new database tables?

-Or does the "_wpcf_belongs_city_id" field still exist, but I need to write something new to it, other than just the parent ID value?

I'm not sure how the new code "toolset_connect_posts( 'posttypea-posttypeb', 5, 7 )" could be made to work in this case. I need to connect the post being created with the city that was chosen in the form. Currently, I grab and store both the new post's ID and the parent city's ID using 2 different hidden fields in the form, so they're available. I just don't know how to add these numbers, which are dynamically created, to the static code above when a form is submitted.

Can you clarify? (Unless, of course, there's a different field I can use to replace the "_wpcf_belongs_city_id", in which case I'll try that.)

#1082748

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I strongly suggest you to look at the following Doc to understand the migration and how to use new post relationship API:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

About Backward Compatibility:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/backward-compatibility-measures-for-post-relationships/

So - whatever Beda suggested in his reply as given under you should follow that:

The magic that Gravity Form does, in this case, is adding the post_meta with key _wpcf_belongs_post_id and value of parent post id to the post you edit.
That will fail with new Relationships, but not with old or migrated relationships.

With new relationships, you'd need to use https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts.
This does not require anymore any code that updates any meta of a post.

Instead, you need to replace the magic of Gravity forms that updates the post meta (probably update_post_meta()) and use something like toolset_connect_posts( 'posttypea-posttypeb', 5, 7 );, where you will change the 5, 7 to the ID's of posts you want to connect.
posttypea-posttypeb is the slug of your relationship or array of post types if it's a legacy relationship
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
#1083983

Thanks Minesh, will look into this further.