save_post is not firing when I only edit custom meta fields

I’ve come across the action hook save_post few days back and I like to take advantage of it.

I’ve custom post post type “Property” with Meta fields associated with it. The problem is that when I make updates to post description and title, then only save_post fires and execute my functionality, but if I make changes to meta fields associated with post without touching description and title, then save_post is not fired.

Read More

What am I missing?

Simple Scenario: save_post fires only when updates made to “Post Title” and “post Description”, but when I edit only meta fields without touching description and title it is not .

Any suggestions?

My Functions.php Codefor hooking save_post .

function myplugin_save_postdata() {
alert('vijay','Event Fired!');
$postid=get_the_ID();

if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $postid ) )
return;
} else {
if ( ! current_user_can( 'edit_post', $postid ) )
return;
}
$old_price = doubleval(get_post_meta($postid, 'REAL_HOMES_property_price', true));
$new_price = $_POST['REAL_HOMES_property_price'];
$vijay=doubleval(get_post_meta($postid, 'REAL_HOMES_property_old_price', true));
update_post_meta($postid,'REAL_HOMES_property_old_price',$old_price);
if($vijay !=''){
$sub_price = $new_price - $vijay;
$dev_price = intval(($sub_price * 100)/$vijay);
update_post_meta($postid, 'REAL_HOMES_property_price_development', $dev_price.'%');
}
}
add_action( 'save_post', 'myplugin_save_postdata' );

Related posts

Leave a Reply

2 comments

  1. The save_post hook is not fired when post_meta is being updated however a filter is available immediately prior to saving of the post metadata: update_post_metadata. That can be used to do this same processing (and the processing can maybe be dropped during save_post).

    function prefix_add_custom_filter_for_postmeta_update() {
        add_filter( 'update_user_metadata', 'myplugin_update_foo', 10, 5 );
    }
    add_action( 'init', 'prefix_add_custom_filter_for_postmeta_update' );
    
    
    function prefix_custom_filter_for_postmeta_update( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
    
        // check if this update is for the key we want.
        if ( 'REAL_HOMES_property_price' == $meta_key && empty( $meta_value ) ) {
            // do your processing of values and updating of other metakeys here.
            // processing....
        }
    
        return null; // this means: go on with the normal execution and save.
    
    }