I've tried almost anything and just can't get this to work.
I have a custom post type with a custom meta field and I want a default value to be added on when the Publish/Update/Save button is pressed.
I tried all kind of hooks wp_insert_post
, save_post
, edit_post
..
add_action('save_post_post_type', 'set_default_meta', 10, 3);
And my function is:
function set_default_meta( $post_ID ) {
$meta_value = get_post_meta($post_ID, 'meta_value', true);
if ( $meta_value == '' && !wp_is_post_revision($post_ID) ) {
add_post_meta( $post_ID, 'meta_value', 0, true );
}
}
When I publish the post, the value is not saved. However whenI click View Post, the value is immediately saved.
Any help will be really appreciated!