0
votes

I have been searching and found a lot of various answers, however, I have not found a definitive answer.

I need to run a function right after a post is done saving to the database. This includes every aspect of the post including post metas. I have tried to hook into save_post but that seems to run my function before post metas are saved. I have also tried post_updated and updated_postmeta, but my function doesn't seem to run on either of them.

Another thing to note, I need to have access to the post ID inside my function.

Edit, My plugin uses the Advanced Custom Fields plugin and the function I have coded uses update_field to either create new post metas or update existing one based on some stuff. This code works. When I run the function at the post_updated hook the function seems to run but nothing happens. If I add die() to the end of my function my code works, but die kills the page and all I am left with is a blank white page at the url wp-admin/post.php. So adding die allows my function to work and I am not sure why it would not work without die.

3
Have you tried post_update with a die() inside your function ? codex.wordpress.org/Plugin_API/Action_Reference/post_updatedAlexis Vandepitte
I put that hook in my post.Tommy Wilkey
Yes, but with a die(). Sometime there are redirections and you will never see the var_dump. And publish_post hook ? It is triggered when publish new post or update postAlexis Vandepitte
OK I added the die() and that seems to have worked but at the same time that die() kills the page after my function is ran and I just get a blank white page.Tommy Wilkey

3 Answers

0
votes

I would comment your post, but I cannot because I dont have 50 rep.

Do you mean the_post? https://codex.wordpress.org/Plugin_API/Action_Reference/the_post

function my_the_post_action( $post_object ) {
    // modify post object here
}
add_action( 'the_post', 'my_the_post_action' );

it ought to have the post Id

https://developer.wordpress.org/reference/hooks/the_post/

0
votes

Okay I found how to make publish_post work.

For custom post type you need to replace the "post" by the post type slug.

Example with the custom post type "Recipe" with "recipe" slug.

add_action('publish_recipe', 'test_publish_post', 10, 2);
function test_publish_post($post_id, $post){

    wp_die($post_id);
}

Don't forget to wp_die() or die(); else you will be redirected and you won't see your var_dump();

0
votes

I was able to fix my issue. It turns out that save_post does seem to run after post metas are saved. My problem actually came from something else inside my code that I was able to fix by changing how I handled that part of my script.