Skip Navigation

[Resolved] Ajax update custom post field value after post save/update

This support ticket is created 3 years, 6 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by xiaolinW 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#2055801

Hello, I'm using save_post hook to automatically add value to a custom field, is there any way to update automatically the value in its input field on post editor page after I save or update the post without refreshing the page?

#2056125

Hello and thank you for contacting the Toolset support.

If I understand well, you would like to refresh the value of the custom field, in the backend after saving the post, right?
I don't think this is possible. Because a new post is created in a different URL than when editing it. This means, that WordPress will refresh the page and won't let a plugin or the theme's code save the post only using AJAX:
- Link to create a new post: hidden link
- Link to edit a post: hidden link

If I did not understand well, can you please elaborate a little more on your question? Maybe using some screenshot and by explaining in steps like:
1. I go to xxx
2. I do xxx
3. I expect to have xxx
4. Instead, I get xxx

What do you store in this field? A calculated value or a default value?

#2056143

hi, thanks for the reply, let me explain more in detail:

1) I created a custom field for a cpt
2) I used save_post hook to automatically set a value everytime I create/update a post, so it means I don't have to insert a value in the field, even if I leave it empty, a value will be saved in the post meta table

Wordpress saves its posts via ajax so the page does not refresh and the custom field does not display the automatically saved value.
Now when I save/update a post, I want this field to retrieve and its value in the field using ajax.

#2056645

Wordpress saves its posts via ajax so the page does not refresh and the custom field does not display the automatically saved value.
This means that the posts are edited using the Blocks editor, right?
But, when the post is created the first time, the editor reloads. For updates, it does not. I expect that the custom field has already a value from the hook that was executed when the post was created.

If it is the Blocks editor, check this StackExchange thread, it explains how to listen for the same event, you can run your custom code at that moment. https://wordpress.stackexchange.com/a/331317

If it is not the blocks editor, I am sorry, I still do not see the steps you are following clearly. I would expect the editor page to reload after each saves unless it has been customized by a 3rd party component(theme or plugins). Would it be possible to record a screencast that shows an example of creating and updating a post?

#2071179

My issue is resolved now. Thank you!