Here I have custom post type called products and it's taxonomy is product_categories . What I need is whenever I add the post in products , I need to call function1, when I update the post then I need to call function2 . How to do this?

I have searched on google and found this solution:

add_action('save_post', 'save_in_filter', 10, 2);
        function save_in_filter($post_id, $post){

 function mynewproduct(){

But what happens is when I click on add new post then suddenly the function1() execute , but what i need is function1 execute after inserting the data not before inserting the data.

When a post is added to products (post_type=products) i need to execute function1(). When a post is updated in products i need to execute function2(); Why post_updated,save_post function not working properly?


This is the original answer but it is little faulty, look below for the updated

You can use following approach.

The my_action_updated_post_meta is executed after the post insert or update is done:

// define the updated_post_meta callback
function my_action_updated_post_meta( $array, $int, $int ) {
    global $post;

    // see your updated post

    // make your action here...
    die('after update');

// add the action
add_action( 'updated_post_meta', 'my_action_updated_post_meta', 10, 3 );

Here's the function's documentation page.


I have notice that the above response is a little faulty - the updated_post_meta action (for post updates) is fired also when opening post for edition in the admin panel (since the _edit_lock is set then), even when no real change is made then.

So I changed the approach.

I use a global variable $my_updated_flag to know that a "real" change has been made. I set this variable to "1" by post_updated action. So if $my_updated_flag == 1 we know that this is not just a _edit_lock change. To differentiate post insert and update action I check the $post->post_status == 'draft' since new posts have "draft" status at this stage.

$my_updated_flag = 0;

// define the updated_post_meta callback
function action_updated_post_meta( $meta_id, $post_id, $meta_key, $meta_value = '' ) {
    global $post, $my_updated_flag;

    if ($my_updated_flag == 1) {
        if ($post->post_status == 'draft') {
        } else {
        $my_updated_flag = 0;
add_action( 'updated_post_meta', 'action_updated_post_meta', 10, 4 );

function action_post_updated( $array, $int, $int ) {
    global $my_updated_flag;
    $my_updated_flag = 1;
add_action( 'post_updated', 'action_post_updated', 10, 3 );
  • why updated_post_meta ? also please tell how can i check post type is products ? May 13 '17 at 7:55
  • As far I understood he needs to execute two functions at different levels. Like execute function1() when he added the post and after inserting the post he is needed to execute function2(). Isn't it @abilasher ?
    – CodeMascot
    May 13 '17 at 8:00
  • I have updated the answer - check the $post object variable - you can get all the post data from it including post_type: $post->post_type.
    – Picard
    May 13 '17 at 8:00
  • @the_dramatist . yes , when a post is added to products (post_type=products) i need to execute function1(). When a post is updated in products i need to execute function2(); May 13 '17 at 8:03
  • Right, that's a good point - I forgot. I've checked it again - when the post is added the function is executed for the first time when the post_status is still draft so you can try to differentiate by this property.
    – Picard
    May 13 '17 at 8:06

have a look at the post_updated hook for example..

// Hook to all private or public post types updating
add_action( 'post_updated', 'my_function' );    

function my_function( $post_id ){

    $post_status = get_post_status( $post_id );

    switch ( $post_status ) {
        case 'draft':
        case 'auto-draft':
        case 'pending':
        case 'inherit':
        case 'trash':

        case 'future':
        case 'publish':
        case 'private':
            // continue


   'do something; 

You can check these links bellow:




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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