OK, I have a solution for you, using a filter wpcf_fields_value_save that can be used to modify the value saved for a custom field at the time it is saved.
So we again use a two-step approach where you first use the toolset_association_created hook to then add a filter for the custom field value.
Here is a generic example that you can modify for your needs. I tested this and it worked both when adding a new connection or connecting an existing post, and it worked whether you provided a value for the custom field or not.
add_action( 'toolset_association_created', 'ts_overwrite_postmeta', 10, 5 );
function ts_overwrite_postmeta( $relationship, $parent_id, $child_id, $intermediate_id, $association_id ) {
if ( true ) { // Add some condition for which associations this should apply to
$default_value = 1234; // Default value
add_filter( 'wpcf_fields_value_save', static function( $value, $field_type, $field_slug ) use( $default_value ) {
if ( 'id' === $field_slug ) { // For this custom field, use the default value
$value = $default_value;
}
return $value;
}, 10, 3 );
}
}
This works perfectly! My issue is resolved now. Thank you a lot! is this a normal behaviour or will the toolset_association_created hook be modified? for me this seems like a complex solution and not expected, specially the static function part, because it's required to know a lot about the toolsets source code. Either way even if it's complex to pull it up the application is pretty simple so thank you again.