Support

Account

Home Forums Front-end Issues understanding acf_form Reply To: understanding acf_form

  • My apologies, I guess I misunderstood and was also in error.

    I thought you were looking for an field group to be based on a taxonomy (standard WP Taxonomy Meta Box), which is not available on the front end form.

    You also mentioned trying to create a field group that was conditional based on a field in another field group, also not possible, either front or back end.

    I also incorrectly said that you could make a field conditional on a tax field, and you’re right that I was wrong. I am sometimes wrong, or half asleep.

    So you can’t have a conditional field based somehow on a taxonomy/term without changing the type of field you’re using for the condition and adding a couple of filters.

    You can accomplish the second field group by using a select field that is dynamically populated from the taxonomy.

    
    add_filter('acf/load_field/name=select_field_name', 
                 'acf_load_select_field_name);
    function acf_load_select_field_name($field) {
        $choices = array();
        $taxonomy = 'my-taxonomy-name';
        $args = array(
            'hide_empty' => false;
        );
        $terms = get_terms($taxonomy, $args);
        if ($terms) {
            foreach ($terms as $term) {
                $choices[$term->term_id] = $term->name;
            }
        }
        $field['choices'] = $choices;
        return $field;
    }
    

    You’ll need to set up the above and load your field group to edit it and it should populate the selections for you. Then you can select the value for the conditional field.

    You’ll also need to set up a acf/save_post hook to set the term when the post is saved, since you’re using a front end form I will include two actions, one for the back end form and one for the front.

    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
    function my_acf_save_post($post_id) {
        // action for back end form
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            return;
        }
        // check to make sure it's the right post type
        // could be combined with the above
        $post_type = get_post_type($post_id);
        if ($post_type != 'my-post_type') {
            return;
        }
        $term_id = get_field('select_field_name', $post_id);
        if ($term_id) {
            wp_set_post_terms($post_id, array($term_id), 'my-taxonomy-name', false);
        }
    }
    

    This next one may not be perfect, a lot of it depends on if your creating a new post or allowing updating of existing posts. I’m assuming a new post here. You can get more information on this hook here http://www.advancedcustomfields.com/resources/acf-pre_save_post/

    
    // action for front end form
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    
    function my_pre_save_post($post_id) {
        // check if this is to be a new post
        if ($post_id != 'new') {
            return $post_id;
        }
        // Create a new post
        $post = array(
            'post_status'  => 'draft',
            'post_title'  => 'A title, maybe a $_POST variable' ,
            'post_type'  => 'my-post-type',
        );
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // your will need the field key of your select field for this next part
        $term_id = $_POST['acf']['field_558aa7b9f6168'];
        if ($term_id) {
            wp_set_post_terms($post_id, array($term_id), 'my-taxonomy-name', false);
        }
    
        // return the new ID
        return $post_id;
    
    }
    

    Hopefully some part of this helps you.