Custom field default value with counter
I have a custom post type called machine and a custom field 'reference_number' what I want is to add a default value to the custom field when creating a new machine and show this value before the save.
Here is what I came up with so far.
function set_default_meta($post_ID){
$current_field_value = get_post_meta($post_ID,'reference_number',true);
// this needs to be like, machine 1, machine 2, ..
$default_meta = 'machine ';
if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
add_post_meta($post_ID,'reference_number',$default_meta,true);
}
return $post_ID;
}
add_action('wp_insert_post','set_default_meta');
The code works and add 'machine ' value to the custom field, but only after saving the post. how can I show it when just starting to add the post. and How can I add a counter variable that increase by one in every new post.
Thanks
I have a custom post type called machine and a custom field 'reference_number' what I want is to add a default value to the custom field when creating a new machine and show this value before the save.
Here is what I came up with so far.
function set_default_meta($post_ID){
$current_field_value = get_post_meta($post_ID,'reference_number',true);
// this needs to be like, machine 1, machine 2, ..
$default_meta = 'machine ';
if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
add_post_meta($post_ID,'reference_number',$default_meta,true);
}
return $post_ID;
}
add_action('wp_insert_post','set_default_meta');
The code works and add 'machine ' value to the custom field, but only after saving the post. how can I show it when just starting to add the post. and How can I add a counter variable that increase by one in every new post.
Thanks
No comments:
Post a Comment