Support

Account

Home Forums Front-end Issues Populate Taxonomy ID dynamically

Solved

Populate Taxonomy ID dynamically

  • Hi

    I am using a frontend form to submit custom post type entries. Everything works fine except I am not able to populate dynamically the category from the select.

    <select id="acf-field_5a903d3132d04" name="acf[field_5a903d3132d04]">
        <option value="1">Foo</option>
        <option value="2">Bar</option>
    </select>
    "new_post" => array(
        'post_type' => 'my_post',
        'post_status' => 'publish',
        'tax_input'     => array(
            'custom_tax_category' => array( 'annuaire_category' ),
            'custom_tax_tag' => array( $_POST['acf[field_5a903d3132d04]'] )
        ), 
    ),

    Any help would be appreciated

  • G’day Mate,

    the form name, name="acf[field_5a903d3132d04]" is representation of acf array with key ‘field_5a903d3132d04’.

    So your value should be:
    $_POST['acf']['field_5a903d3132d04'] 😉

    Cheers

  • Thanks but unfortunately it does not work

    'new_post' => array(
        'post_type' => 'annuaire',
        'post_status' => 'publish',
        'tax_input'     => array(
            'custom_tax_category' => array( 'annuaire_category' ),
            'custom_tax_tag'      => array( $_POST['acf']['field_5a903d3132d04'] )
        ), 
    ),

    Following code is well returning the select value when form is submited but it seems that data is not send with form.

    $test = $_POST['acf']['field_5a903d3132d04'];
    echo 'value ' . $test;
  • If the data isn’t sent from the form, you can dump out the post value before your code and see what’s the actual key is. (maybe there’s a typo in your field key)

    
    echo '<pre>';
    print_r($_POST['acf']);
    echo '</pre>';
    

    Another reason might be, the form is sending the value as a string, but the wp functions requires a pure integer. See last example in the doc, https://codex.wordpress.org/Function_Reference/wp_set_post_terms

    So, your code might even need to cast the value with intval first.

    
    'custom_tax_tag => array( intval($_POST['acf']['field_5a903d3132d04']) )
    

    Cheers

  • I have open a new thread https://support.advancedcustomfields.com/forums/topic/post-select-value/ because your code was good and helpful to get the taxonomy by key_field but I have another issue to make it set the category on my cpt.

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

The topic ‘Populate Taxonomy ID dynamically’ is closed to new replies.