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?
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?
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.
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?
My issue is resolved now. Thank you!