Skip Navigation

[Resolved] When using the hook toolset_association_created i'm not able to update post meta

This support ticket is created 4 years, 4 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 16 replies, has 3 voices.

Last updated by Martín Mellado 4 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1763537

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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 );
    }    
}

Please let me know how you get on.

#1764345

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.