0

I am having a difficult time trying to figure out how to save user meta data to a post.

The goal that I am trying to accomplish is this, have a user make a selection from a drop down list, and save his selection. When the user come back to that page it loads his selection that he saved from last time. My first idea was to do this by adding meta data. So essentially what Ill have is below.

'$user_selection = array($user_ID => $the_users_choice)'

updating and adding this to meta is where I am having problems. I need the $user_ID to be unique, if it doesn't exist in the meta, then wordpress will add it, if it does then wordpress updates the value ($the_useres_choice). At the moment it just keeps adding the value to the end of the array. Ive attached my entire code below.

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    if (isset ($_POST['charity_name_form'])) {

        $title =  $_POST['charity_name_form'];
        $user_ID = get_current_user_id();
        $user_choice = array($user_ID => $title);

        add_post_meta(get_the_ID(), 'user_charity', $user_choice);


    } else {
        echo 'Please Select a Charity;
    }

}
  • I think you should be using update_post_meta() instead of add_post_meta? – jimihenrik Mar 29 '15 at 15:18
  • @jimihenrik this works for one user however once the second user makes his selection and submits, it removes the first users selection and relics it with his. The user ID would be unique and the charity he picks would be a value he decides on. – Derrick Workman Mar 29 '15 at 18:53
  • I may be attacking this problem at the wrong angle to begin with. I am not totally sure. – Derrick Workman Mar 29 '15 at 18:55
  • Oh sorry, I really didn't read your description properly the first time. But yeah, ofc you can't update the posts meta because there's only one meta field. You would need multiple meta fields like userx_setting, usery_setting etc... Better would probably be to add this as a profile setting for the user maybe or something.. – jimihenrik Mar 29 '15 at 19:06
  • @jimihenrik Yes I thought about doing that at first, however the meta is not attached to the user its based on the post. So deepening on the post the user can select different options. – Derrick Workman Mar 29 '15 at 19:10
2

So I was really overthinking this problem. Hopefully my answer can help someone in the future. Basically all I needed to do was save the array from wordpress and check to see if the user_ID (Associative Array Key) was in the Array, if it was and the user needed to change the value I needed to search throughout the array replace the value and update that array. If the User has never made a selection before, just needed to add his selection to the array.

<?php 
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['charity_name_form'])) {

        $title =  $_POST['charity_name_form'];
        $user_ID = get_current_user_id().'_ID';
        $user_choice = array($user_ID => $title);


        $meta = get_post_meta( get_the_ID(), 'user_charity');
        $array_location = $meta[0];
    // THIS CHECKS TO SEE IF USER HAS MADE A SELECTION BEFORE, IF HE HAS AND UPDATES HIS SELECTION THE VALUE CHANGES
        if (isset( $array_location[$user_ID])){ 

            $new_array = array();
            foreach ($array_location as $key => $values){
                $new_array[$key] = $values;
            }
            $new_array[$user_ID] = $title;

            update_post_meta(get_the_ID(), 'user_charity', $new_array);

        }
        // IF USER NEVER SELECTED A CHARITY HE SELECTS ONE
        else {
        // TAKES OLD ARRAY AND PUTS IT IN NEW EDITABLE VARIABLE
            $new_array_add = array();
            foreach ($array_location as $key => $values){
                $new_array_add[$key] = $values;
            }
            // ADDS THE NEW USER SELECTION TO THE PREVIOUSLY SAVED ARAY
            $new_array_add[$user_ID] = $title;
            // UPDATES THE META WITH THE NEW ARRAY VARIABLE
            update_post_meta(get_the_ID(), 'user_charity', $new_array_add);
        }      
    }

}

?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.