I’m trying to update user meta but is always 1, What I doing wrong? [closed]

Your user_meta is not updated, because you process checkboxes incorrectly…

First of all… You’ve set values of all your checkboxes to 1. So if they’re checked, then their value is 1 – and this is the value you save as user_meta.

On the other hand, when the checkbox is not checked, then it won’t be sent in POST request. So your foreach loop won’t loop through them – so the user_meta will never be set to anything other than 1 ๐Ÿ˜‰

How to fix it?

You shouldn’t iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).

Also, you should not trust user inputs so much – what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ๐Ÿ˜‰

So I would do it like so:

function save_consultation_user_profile_fields( $user_id ) {
    # save my custom field
    if( ! current_user_can('manage_options') )
        return false;

    $query = new WP_Query(  // it's the same query you use to create checkboxes in form
        array(
            'posts_per_page' => -1,
            'post_type' => 'consultation',
            'post_status' => 'publish'
        )
    );
    foreach ( $query->posts as $post ) {
        if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
            update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
        } else {
            delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
        }
    }
}

This way you’ll be sure, that users can gain access only for existing posts. You will be nicer for DB also – if user has no access to post, then no meta is stored in DB.