Skip Navigation

[Resolved] Trying to update a Custom Field when adding a related post (admin)

This support ticket is created 2 years, 2 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 4 replies, has 2 voices.

Last updated by Nigel 2 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#2296165

I am trying to update a Custom Field when we add a new related post.

1. Add a Post of type "parent"
2. Save the post of type "parent"
3. Now add a new Child Post, while inside the "parent" post edit screen, using the Toolset "Related Posts" Metabox
4. In the related post, there is a Custom Field.
5. That custom field should be updated programmatically.

It works just fine when I update the related post while inside the related post edit screen.
When I however add the post from within the Related posts Metabox, the field is not updated.

I tried Several code approaches.
The one that works while inside the related post edit screen (save_post at a late priority), does not work.
Then I also tried to hook into updated_post_meta, as well at a late priority, and also it does not work.

I tried at PHP_INT_MAX priority, without luck.

What is the correct hook or code to update a Custom Field when we save the related post from the Related posts Metabox?

Attempted code:
1. Works just fine when inside the related post edit screen:

add_action( 'save_post', 'update_termin_with_angebot_data', 99, 3 );
function update_termin_with_angebot_data( $post_id, $post, $update ) {
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )  {
      return;
  }
  if ( ! current_user_can( 'edit_post', $post_id ) ) {
    return;
  }
  update_post_meta( $post_id, 'wpcf-monat', 'my-value');
}

2. Works as well if hooked to updated_post_meta (if inside the related post edit screen)
3. Neither works if we add the post from within the related post Metabox.
==> All hooks have been tried as well with PHP_INT_MAX

#2296687

Nigel
Supporter

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

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

Hi there Beda

I'm nosing around in the source code and it looks like the related post is created/updated, some code happens, and then the field values are set.

So if you try to use a save_post hook or anything relating to updating the post itself to trigger your code which sets up the post meta, then whatever you did is going to be overwritten a few lines of code later.

You can only use the hooks related to updating the post meta (e.g. update_{$meta_type}_meta, update_postmeta, or updated_{$meta_type}_meta and updated_postmeta).

If you place a breakpoint at types/vendor/toolset/types/embedded/classes/field.php:386 then you see when the saving of the custom field actually occurs.

Hopefully that's enough to put you on the right track, but let me know if not...

#2297219

I recall as well that in past while debugging I saw some weird stuff going on in the code (actually Toolset copies, deletes and then re-saves all meta each time you save a post, for some WPML compatibility issue, if I recall it right).
I have also hooked the updated_post_meta to a very late (PHP_INT_MAX) priority, and still I don't get the field saved.

However, what I see when I log what field is updated on updated_post_meta...
it shows me only ONE of the fields that I actually have in the related post Metabox!

And ... it is only the field I do NOT set when I add the related post.
The one I want to update programmatically. All other fields are not logged during the updated_post_meta hook, at any priority.
And, they are also not available from get_post_meta at this point, so it is impossible for me to get the value from the database, and update that field which is updated (even if I do NOT update or set that field during adding the related post...)

Plus, an _edit_lock field (if I do a very high priority). Otherwise, nothing there.

That makes no sense to me, it shouldn't be that _not set_ fields get logged, but the set fields don't, and are not even in the get_post_meta at this point?

Did you get a hook to work that takes values from Field 1 and puts them into field 2, when field 2 is NOT set during adding the related post, in a related posts Metabox?

#2298987

Nigel
Supporter

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

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

Sorry Beda, we just fixed a systems issue that has seen a flood of tickets return to the queue that need dealing with.

It will take me a while to dig into what's going on here, so please bear with me, I should be able to do that tomorrow.

#2302047

Nigel
Supporter

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

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

Going a little deeper down the rabbit hole I found custom hooks that Types fires after saving the custom fields, and in the quick tests I ran the hooks do get triggered from the related posts UI, whether or not you supply a value for the custom fields, so I think you should be able to use those.

Here are how the hooks are set up in types/vendor/toolset/types/embedded/classes/field.php:461 (so you can see the parameters available):

        do_action( 'wpcf_fields_save', $value, $field, $this, $meta_id, $meta_value_original );
        do_action( 'wpcf_fields_slug_' . $field['slug'] . '_save', $value, $field, $this, $meta_id, $meta_value_original );
        do_action( 'wpcf_fields_type_' . $field['type'] . '_save', $value, $field, $this, $meta_id, $meta_value_original );

When trying to overwrite values entered in the UI, this worked:

add_action( 'wpcf_fields_slug_first-field_save', 'ts_field_action', 10, 5 );
function ts_field_action( $value, $field, $_this, $meta_id, $meta_value_original ){

    update_post_meta( $_this->post->ID, $field['meta_key'], "Overwritten!" );
}

So... I think you have enough there to achieve what you want...

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