Skip Navigation

[Resolved] Creating of intermediary_id in wp_toolset_associations

This support ticket is created 5 years, 11 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 Alex 5 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1172047
intermediary_id.jpg

Hi,

I want to move postmeta data from a parent post type to an intermediary post type programmatically with PHP.
How can I generate the intermediary post type to and get the intermediary_id in wp_toolset_associations table?

First
I created the m2m relationship between two custom post types and added an intermediary post type plus custom fields with toolset.
Now I can add and update data in the backend to the custom fields in the intermediary post type.
But I have hundreds of custom fields of a specific meta_key and meta_value which I want to move to the intermediary post type custom fields programmatically with PHP. I don´t wand to use an importer for this data.

I saw that there is a link between the two post types in the table wp_toolset_associations but the intermediary_id is only generated, when I save one of the post types in the wordpress backend.

Is there a way I can create the intermediary post type with id in PHP?

Thank you

#1172249

Hi Alexander,

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

Your requirement is divided into two parts and for this reason, you'll need to create two custom functions:

1. To programmatically connect/join two posts in a relationship "toolset_connect_posts" function can be used:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Note: this function will not provide you an intermediary post's ID.

2. The post-relationship API includes a hook "toolset_association_created" which can be used to execute a specific function to update/move the custom fields from the parent post to the intermediary post:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

As shown in the documentation, this IDs for the parent, child and the intermediary posts are available, when a function hooked to this function is executed.

I hope this helps.

regards,
Waqar

#1172267

Thank you Waquar!

Step 2 works.

Step1 I get the Fatal error: Uncaught Error: Call to undefined function toolset_connect_posts() ...
I am using this function in my plugin code.
Do I have to register this function somewhere?

Regads

Alex

#1172533

Hi Alexander,

Thanks for the update.

The error "Call to undefined function..." suggests that your plugin is calling the "toolset_connect_posts" function before Toolset Types plugin has fully loaded.

To avoid this you can delay its execution until all plugins have loaded, using the "plugins_loaded" hook:

Example:


add_action( 'plugins_loaded', 'my_plugin_override' );

function my_plugin_override() {
    toolset_connect_posts( $relationship, $parent, $child );
}

( ref: https://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded )

regards,
Waqar

#1172540

My issue is resolved now. Thank you!