Skip Navigation

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

This support ticket is created 3 years, 8 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+01:00)

This topic contains 16 replies, has 3 voices.

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

Assisted by: Nigel.

Author
Posts
#1745485
result.jpg

Hello,

Tell us what you are trying to do?
I'm using the hook toolset_association_created to update post meta of all involved parts in 2 relationships between 3 custom post types. I use a similar hook also when creating any of them to ensure that those fields are always created.

When creating them there is no problem and i can update the post meta but when i use the hook i'm only able to update any of the involved ids (parent, child or intermediary).

I created a cpt to keep track of the debugging and what i can see is when i create the relationship this non related cpt can be created and updated inside the hook. The hook let me get the post meta related to each of the post but not updating them (the result is in the image). The result when i try to update is false.

my code looks like this right now

add_action( 'toolset_association_created', 'crear_ids_relaciones', 10, 5 );
function crear_ids_relaciones( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {

global $database;
$parent_post = get_post_type($parent_id);
$child_post = get_post_type($child_id);
$ids = $database->select("wp_postmeta", "meta_value", ["meta_key" => "wpcf-id"]);
rsort($ids);
$nuevoid = $ids[0] + 1;

$resultado = array(
"Parent" => "El parent es el cpt ".$parent_post." con el id ".$parent_id,
"Child" => "El child es el cpt ".$child_post." con el id ".$child_id,
"Nuevo id" => "Si se pudiera escribir el nuevo id sería ".$nuevoid,
"Resultado condicion id padre" => get_post_meta($parent_id, 'wpcf-id',true),
"Resultado condicion idc padre" => get_post_meta($parent_id, 'wpcf-idc',true),
"Resultado condicion id hijo" => get_post_meta($child_id, 'wpcf-id',true),
"Resultado condicion idc hijo" => get_post_meta($child_id, 'wpcf-idc',true),
);

if(empty($resultado['Resultado condicion id padre'])){
$actualizarIdPadre = update_post_meta($parent_id, 'wpcf-id', $nuevoid);
} else {
$actualizarIdPadre = update_post_meta($parent_id, 'wpcf-id', $resultado['Resultado condicion id padre']);
}
if(empty($resultado['Resultado condicion idc padre'])){
$actualizarIdcPadre = update_post_meta($parent_id, 'wpcf-idc', encriptarMicroSegundoUrl());
} else {
$actualizarIdcPadre = update_post_meta($parent_id, 'wpcf-idc', $resultado['Resultado condicion idc padre']);

}
if(empty($resultado['Resultado condicion id hijo'])){
$actualizarIdHijo = update_post_meta($child_id, 'wpcf-id', $nuevoid);
} else {
$actualizarIdHijo = update_post_meta($child_id, 'wpcf-id', $resultado['Resultado condicion id hijo']);
}
if(empty($resultado['Resultado condicion idc hijo'])){
$actualizarIdcHijo = update_post_meta($child_id, 'wpcf-idc', encriptarMicroSegundoUrl());
} else {
$actualizarIdcHijo = update_post_meta($child_id, 'wpcf-idc', $resultado['Resultado condicion idc hijo']);
}

// ? DEBUG
$nuevo_debugger = array(
'post_type' => 'debugger',
'post_title' => "Relación ".$association_slug." creada",
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
);
$nuevo_debugger_id = wp_insert_post($nuevo_debugger);
$resultado2 = array(
"Resultado id padre" => $actualizarIdPadre,
"Resultado idc padre" => $actualizarIdcPadre,
"Resultado id hijo" => $actualizarIdHijo,
"Resultado idc hijo" => $actualizarIdcHijo,
);
update_post_meta($nuevo_debugger_id, 'wpcf-resultado-debug', json_encode(($resultado+$resultado2), JSON_UNESCAPED_UNICODE ));

return true;

}

#1746131

Nigel
Supporter

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

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

Hi there

I'm looking at your code, but I don't understand what it's trying to do. We don't support custom code, but I can help you understand how the API functions work.

The hook toolset_association_created will be triggered whenever two posts are connected using a Toolset relationship.

The available parameters provide the slug of the relationship, the id of the connected parent (or the left side of a M2M relationship), the id of the connected child (right side of M2M), and if this is a M2M relationship which creates an intermediate post, its id. (The id of the connection is also available, but unlikely to be useful in most circumstances.)

Armed with the ids of each of the posts you can then get the post objects, get existing postmeta belonging to the posts, or update postmeta for any of the posts, using the standard WordPress functions.

You can use the following simple code to verify that the hook is working as expected:

add_action('toolset_association_created', 'probar_toolset_associations_created', 10, 5);
function probar_toolset_associations_created( $relationship, $parent_id, $child_id, $intermediate_id, $association_id ){

    error_log( "relationship:" . $relationship );
    error_log( "parent_id:" . $parent_id );
    error_log( "child_id:" . $child_id );
    error_log( "intermediate_id:" . $intermediate_id );
    error_log( "association_id:" . $association_id );

}

It will print each of the arguments to the debug log where you can confirm the correct values are being provided.

If that's the case then the problems would be in the logic of your subsequent code.

#1748621
wpadmin.jpg
resultdebugDB.jpg
resultdebugConsole.jpg

Thank you Nigel. the function i created also get those same elements. I actually can get all the 5 parameters right, but when i try to
use the update_post_meta function, in the console shows it's updated during the hook, but after the hook there is no post meta updated.

Reducing what i'm trying to something simple

add_action( 'toolset_association_created', 'crear_ids_relaciones', 10, 5 );
function crear_ids_relaciones( $relationship, $parent_id, $child_id, $intermediate_id, $association_id ) {

    $nuevoid = 1500;
    
    error_log( "id:" . $nuevoid );
    error_log( "relationship:" . $relationship );
    error_log( "parent_id:" . $parent_id );
    error_log( "child_id:" . $child_id );
    error_log( "update_meta_child_id:" . update_post_meta($child_id, 'wpcf-id', $nuevoid));
    error_log( "updated_meta_child_id:" . get_post_meta($child_id, 'wpcf-id', true));
    error_log( "intermediate_id:" . $intermediate_id );
    error_log( "association_id:" . $association_id );

    return true;

}

In my current configuration i just run toolset and wordpress, no extra plugin.

This happens when i use in the editor the "Add new" button. Is the exact same problem in this previous support tickets. There is no explanation about the solutio but i'm having the same problem.

https://toolset.com/forums/topic/toolset_association_created-when-adding-new-instead-of-connect-existing/

#1753473

Nigel
Supporter

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

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

OK, I see the problem.

When you use the Add New rather than Connect Existing button, it seems the post meta get overwritten, i.e. the toolset_association_created hook gets triggered after the post is created but before any custom fields are saved.

I need to look into this a little further to see if there's a way to have the hook fire later, or I may have to consult with the devs.

Give me a little while and I'll get back to you.

#1753951

Yes, that's exactly what's happening. Thank you for looking into this.

#1756307

Nigel
Supporter

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

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

I've been studying the logic of the source code for Types, and when you insert a new related post with that UI there is no escaping that the post is inserted, the toolset_association_created hook is triggered, and then custom field values are saved.

I've put in a question to the developers to see if they can recommend some alternative that would circumvent this issue, failing which they may be able to make some modification to the code, though that would take time to implement.

I'll get back to you (again).

#1756669

I hope it can be solved soon. I got this error before and my only solution was to use the save_post hook with any of the elements in a relationship. Like fixing the relationship when any of it's parts is updated, but is a bit annoying because it's an extra step and one of the features of that relationship GUI (in my opinion) is not having to interact again with the parent or child post, and i've also forgotten to update a few times so it's prone to erros. I checked into this removing every hook and also trying other hooks but nothing seems to update the meta fields in that particular interaction.

#1758137

Minesh
Supporter

Languages: English (English )

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

Nigel is on Vacation today. He will get back to you tomorrow as soon as possible.

#1758221

Ok, thank you for the information

#1758959

Nigel
Supporter

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

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

Sorry to keep you waiting.

There is a hook which fires after a Types custom field value is saved, namely wpcf_fields_save.

So we can overwrite the custom field values that are saved when a new associated post is created by using the toolset_association_created hook to then add an action to the wpcf_fields_save hook where we overwrite the newly saved value with the value we choose.

Let me show you a generic example, and then you can adapt it to your needs.

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 ) {

        global $new_value;
        $new_value = 1234;

        add_action( 'wpcf_fields_save', 'ts_field_saved', 100, 3 );
    }    
}

function ts_field_saved( $value, $field, $object ){

    global $new_value;

    $post_id = $object->post->ID;
    $meta_key = $field['meta_key'];

    update_post_meta( $object->post->ID, $field['meta_key'], $new_value );
}

You'll note I've added the a dummy test in the first function (if true) which you can replace with a meaningful test (to check that you are only doing this for a particular relationship, for example, because you may have other relationships that you don't want this to happen for).

#1759627
postrequest.jpg

Thank you Nigel. The hook you mentioned only works to update the $field parameter, not another meta field. I thought why so i checked toolset's code and i think this happens because is triggered when a single field is updated, so the field need to have something written, and if i want to calculate something without an input is not possible. I think the problem is related to the order of the post request from the relationship form. I get to update the fields before the post request because maybe it's only updating what was updated, and the emptying anything that is blank with the post request. It doesnt matter what i do with the hooks if the form is triggered after any other action. I think the hook is before where its needed.

Thank you for your time

#1760053

Nigel
Supporter

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

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

OK, yes, in my testing I was overwriting values that were entered in the UI to add a new post, I wasn't considering fields that were left blank.

And testing, I can confirm that where such fields are left blank the wpcf_fields_save hook doesn't get triggered.

I've asked the lead Types developer for advice on any other hook that could be used, and I'll get back to you.

#1762361

Hello. Is there any news about this issue? Thank you

#1762363

Nigel
Supporter

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

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

Not yet, I'm afraid.

I created an internal ticket for the lead Types developer with the details on Friday and this morning they said it was in their inbox for today. As soon as I get some feedback from them I'll share it with you.

#1762369

Ok, thank you for the information.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.