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.
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' );
You can try update button on the post after changing the metafields which will fire the save post action…
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 duringsave_post
).