1

I am new with plugin development and i have one issue while updating post meta. I have to create checkbox for each user but while updating post update_post_meta is not working for all checkboxes. Values are correct but these are not updating in database. Here is my code.

add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
global $post;
$all_users = get_users();
foreach($all_users as $user){
$my_meta_box_check = "my_meta_box_check".$user->ID;
$chk = isset( $_POST[$my_meta_box_check] ) && $_POST[$my_meta_box_check] ? 'on' : 'off';
update_post_meta( $post_id, $my_meta_box_check, $chk );
}
}

This code is working for few check boxes but not for all. I will really appreciate your help because i am trying to fix this issue for many hours but nothing working.

  • Maybe this plugin will help you to handle data from metabox wordpress.org/plugins/meta-box – Alexey Jul 18 '17 at 19:59
  • Thank you for your suggestion. But i could not use plugin because i have already lot of other functionality related to these checkboxes. – wplearner Jul 18 '17 at 20:04
  • You are not setting $post_id variable. You need to add $post_id = $post->ID; after global $post – czerspalace Jul 18 '17 at 20:53
  • $post_id is already in functions parameters and i have also tried with your suggestion but still the same problem. – wplearner Jul 18 '17 at 21:07
  • How many users are there? I wonder if get_users() just returns you first 10 or something... What about adding paged param to it to make it 100% sure it gets all users? get_users(array('number' => 10000)) – Worduoso Jul 18 '17 at 21:15

Your Answer

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

Browse other questions tagged or ask your own question.