I want to run a function A() when a post is published and function B() when the same post is edited or updated.
For this, I found publish_post action which is triggered whenever a post is published, or if it is edited and the status is changed to publish.
How can I use this publish_post action to know that post has been edited or updated so that I can run function B()?
I want to run a function A() when a post is published and function B() when the same post is edited or updated.
For this, I found publish_post action which is triggered whenever a post is published, or if it is edited and the status is changed to publish.
How can I use this publish_post action to know that post has been edited or updated so that I can run function B()?
Share Improve this question asked Jul 1, 2017 at 17:18 busyjaxbusyjax 5391 gold badge5 silver badges18 bronze badges2 Answers
Reset to default 2With the post_updated
hook you can trigger an action when the post is updated. He passes 3 parameters:
$post_ID
(the post ID),$post_after
(the post object after the edit),$post_before
(the post object before the edit)
Here's an example:
<?php
function check_values($post_ID, $post_after, $post_before){
echo 'Post ID:';
var_dump($post_ID);
echo 'Post Object AFTER update:';
var_dump($post_after);
echo 'Post Object BEFORE update:';
var_dump($post_before);
}
add_action( 'post_updated', 'check_values', 10, 3 ); //don't forget the last argument to allow all three arguments of the function
?>
See reference Codex
You could achieve this with save_post hook.
Example similar to code in codex
function run_my_function( $post_id ) {
if ( wp_is_post_revision( $post_id ) ){
// if post udpated
} else {
//if is new post
}
}
add_action( 'save_post', 'run_my_function' );