Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom post type called 'cards' and I want Users to be able to 'level up' the Cards, so I have 5 custom fields on the Cards post type (1 for each Level). These fields are an ACF User field with the return value set to array and multiple values allowed.

Initially, all Users are added to the Level 1 field since the Cards for all Users will start at Level 1 by default.

What I want to achieve is that on submission of a form on a single Card post, assuming the user is in the Level 1 field for the current Card, a function runs that finds the current user in the Level 1 field and adds that User to the Level 2 field, but then also removes them from the Level 1 field. If they were found in the Level 2 field, it would add them to the Level 3 field and remove them from the Level 2 field, and so on. The idea being that the same user can only appear in each of the Level fields once.

What I have tried:

This is the current code I have:

PHP
add_action('acfe/form/submit/post/form=level-up-card-form', 'my_level_up_card_form_custom_action', 10, 5);
function my_level_up_card_form_custom_action($form, $action){
    
$post_id = get_the_ID();
$current_user = get_current_user_id();
$level1 = get_post_meta($post_id, 'level_1_users', true);
$level2 = get_post_meta($post_id, 'level_2_users', true);
$level3 = get_post_meta($post_id, 'level_3_users', true);
$level4 = get_post_meta($post_id, 'level_4_users', true);
$level5 = get_post_meta($post_id, 'level_5_users', true);

    if(in_array($current_user, $level1)){
        delete_post_meta($post_id, 'level_1_users', $current_user);
        update_post_meta($post_id, 'level_2_users', $current_user);
    }
    elseif(in_array($current_user, $level2)){
        delete_post_meta($post_id, 'level_2_users', $current_user);
        update_post_meta($post_id, 'level_3_users', $current_user);
    }
    elseif(in_array($current_user, $level3)){
        delete_post_meta($post_id, 'level_3_users', $current_user);
        update_post_meta($post_id, 'level_4_users', $current_user);
    }
    elseif(in_array($current_user, $level4)){
        delete_post_meta($post_id, 'level_4_users', $current_user);
        update_post_meta($post_id, 'level_5_users', $current_user);
    }
    elseif(in_array($current_user, $level5)){
        return;
    }
    
}


This is adding the current user to Level 2 field for example but not removing them from the Level 1 field. But it's also not correct because it's replacing the whole field ONLY with that current user, rather than inserting them.

Again, I want the current user ONLY to be added to the next Level field and removed from the previous Level field. How can I achieve this? Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900