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
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
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
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
My issue is resolved now. Thank you!