Home Forums General Issues Pre-Populate Radio Button Choices


Pre-Populate Radio Button Choices

  • Hello All,

    After much searching and trial I have to ask for some insight/guidance.

    ISSUE: I am trying to store Choice Options for a Radio Button on Post Creation from a Function.

    SO FAR: The function successfully creates a new Record for a Post Type (ANSWERS) when called by the user and inserts a user response (text) into a new row of the repeater field.

    ANSWERS contains a Text field name USER_NAME.
    ANSWERS contains a Repeater field named USER_RESPONSE, which contains two fields.

    • USER_ANSWER is a text field
    • USER_SCORE is a radio button, which has a single place holder choice, NULL allowed is TRUE

    Here is a walk though of my code and logic so far:

    function create_answers ()  {
    $title = date('m-d-Y_g:i:s');
    $user_name = get_user($current_user);
        $new_post = array(
          'post_title'    => $title,
          'post_status'   => 'publish',
          'post_type'     => 'answers'
        // Save the new post
        $pid = wp_insert_post($new_post);
       // Add the current user display name
    	$rid = 'user_name';
    	$value = $user_name;
    	update_field( $rid, $value, $pid );
       // Add a new row(s) to the Repeater
       // code removed that goes and gets the passed variables to keep this post short. 
           $rid = 'user_reponse';   // Repeater Field Name
           $row = array (
                  'user_answer' => $answer_passed_from_form)
          	add_row($rid, $row, $pid);

    Works as intended so far … post is created successfully and the text field USER_ANSWER is populate with the passed variable.

    Now I want to be able to add the USER_SCORE Radio Button Options at this point.

    For this entry I want to use: [LOW]=>1, [MED]=>5, [HIGH]=>12


    Inserting the data using the add_row function above.

    I have tried testing with a single choice using the update_sub_row function

    $value = "[choices][LOW] = '1'";
    update_sub_row('user_score', 1, $value);

    This returns FALSE and is confirmed with viewing the post and the choice is not there.

    I believe I am not crafting the $value correctly and when passed to the the field it is failing.

    Help is appreciated.

    NOTE: The choices need to be stored in the field for future “selection”, so unable to use a filter or such at load time. The choices may change based on a Category or some other condition, and that condition may or may not exist later.

  • The value of the radio field is the wrong thing to update. What you need to do to do this is update the fields choices. I don’t think that you can change a fields settings dynamically, well you can but during the acf/load_field, not when updating values.

    The bigger issue is that also want the same radio field to allow different choices depending on the question. You can’t do this. One radio field will always have the same choices. I don’t think that there is a way to do what you’re trying to do.

  • John – Thank you for the virtual look over the shoulder. I am trying to save the Choices to the field in the post and now I am realizing based on your response that the information stored in the post field is the Selected Value.

    What I get to for coding tired.

    Having said that, I think I have a solution. I can add a Third field that stores the Choice List and then at load time, Render the field with those choices and allow the choice to me made and stored as intended on Save.

    Appreciate the help, off to test this idea.

  • The idea is a good one, but because you are using a repeater and the radio field is a sub field of the repeater, you can’t use acf/load_field. This will make all of the radio fields have the same choices.

    The can be done but you’re going to need to do it in JavaScript, and there might still be a problem getting the selected value to actually be selected. The reason being that if the selected value is not one of the choices when ACF loads the field then it will set none of them or the default to be currently selected for each of them and you’ll need to re-select the choice every time. Basically, in JavaScript when the page is ready you will need to read the values for your choices, then populate the radio field with your choices, the you’ll need to do an AJAX request to get the currently selected choice, if any and set the right radio button to be selected.

    You might want to take a look at some of my examples for dynamically generating field values in JS

    Here are something you won’t find there

    // get the row that a field is in
    var $row = e.$el.closest('.row');
  • Thank you for the assistance with this. This was the solution I needed.

  • I’m glad that the information helped you

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.