2

I want to check if the title of the post has changed and do some stuff if it did.

I thought of using the post_updated hook, the codex says "Use this hook whenever you need to compare values before and after the post update.", but i can't figure out how to use the before and after arguments.

function check_values($post_ID, $post_after, $post_before){
        echo 'Post ID:';
        var_dump($post_ID);

        echo '<br>Post Object AFTER update:';
            echo '<pre>';
            print_r($post_after);
            echo '</pre>';

        echo '<br>Post Object BEFORE update:';
            echo '<pre>';
            print_r($post_before);
            echo '</pre>';
    }

    add_action( 'post_updated', 'check_values', 10, 3 );

and i'm echoing it echo check_values($post->ID, get_post(),get_post()); inside a metabox.

Currently I'm getting the same values.

If i echo check_values(); I get missing argument error and on echo check_values($post_ID, $post_after, $post_before) i get NULL.

How can i get the values before and after the update?

Update

function check_values($post_ID, $post_after, $post_before){
    if ($post_before->post_title != $post_after->post_title) { 
        // do stuff
    }   
}
add_action( 'post_updated', 'check_values', 10, 3 );
2
  • compare $post_before and $post_after like if ($post_before->post_title != $post_after->post_title) { //title changed! } Jan 6, 2016 at 8:30
  • 1
    wait... you're doing it wrong... you can't just echo check_values($post->ID, get_post(),get_post()); inside a metabox... this function is an action when a post is saved or updated.. you need something else if you want to check the title of the post Jan 6, 2016 at 8:33

2 Answers 2

6

First off you are using the function wrong. The function is tied to an action within wordpress whenever you use the add_action function. In this case you have added it to the post_updated action.

Basically your check_values function gets added to a queue which will then get called by the action when its time to do the action.

You can see where the action gets called here: https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/post.php#L3359

See line 3359 of this file. There is a function called do_action and its going to call all the functions tied to the post_updated action. This would include your check_values function. This is where your function is getting called and as you can see it passes in the necessary parameters for your function to interact with.

Try updating or adding a post. You should see some output there because this is when wordpress is calling your function.

Then in order to check to see if there was a change compare the two titles from the two before and after post objects

<?php 

function check_values($post_ID, $post_after, $post_before){
    if( $post_after->post_title !== $post_before->post_title ) {
        // do something
    }
}

add_action( 'post_updated', 'check_values', 10, 3 );
?>
2
  • Thank you @JoeMoe1984. I kind of knew how action/hook work but i wanted to see the difference, that's why i echoed it. Got it working now.
    – CK13
    Jan 6, 2016 at 9:42
  • @CK13 Ok, it just seemed like you were confused about how it worked because you tried a few things that would not have worked. In that case I will update my answer.
    – JoeMoe1984
    Jan 6, 2016 at 9:45
0

When I used this action "post_updated" and update the post from latest updated gutenburg editor that time I get same post name in before post and after post details array.

It means suppose post name is "abc-test" and I changed this to a new post name as "abc-test-2" then I get "abc-test-2" name in both post after and before details.

Why its showing same data?

Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.