Support

Account

Home Forums Front-end Issues understanding acf_form

Solved

understanding acf_form

  • Hi,

    Despite great efforts, I can’t understand how acf_form() works.

    First of all, I create two fields groups :
    main: main group with main fields, displayed when “Post type is equal to document” OR “Page is equal to add a document
    secondary: another, only display when “Post taxonomy is equal to a_specific_kind_of_doc“.

    On backend, when entering in document post type, it works. I have the form and secondary group is displayed only when I select a specific option.

    But on frontend, I just have… nothing but a submit button.
    I done this :

    									$options = array(
    	'field_groups' => array('group_54dcecbb2f254','group_54ddd77bb7f6b'),
    	'post_id' => 'new_post',
    	'new_post'		=> array(
    		'post_type'		=> 'post',
    		'post_status'	=> 'pending',
    	),
    	'submit_value' => __('Add this document', 'xxx'),
    	'updated_message' => __('Document added.','xxx'),
    );
    acf_form($options);
    
    

    I also try to add

    
    <code>acf_enqueue_uploader();</code>
    

    and

    
    <script type="text/javascript">
    (function($) {
    
    	// setup fields
    	acf.do_action('append', $('#popup-id'));
    
    })(jQuery);
    </script>	
    

    even if I don’t really understand purpose of that.

    So, I have to add the field_groups in $options, then it works, obviously… but the secondary field groups is not shown when I select the specific option.
    And if I add my secondary field_groups to options, it actually appears but always, not only when my specific option is selected.

    In short, It lacks concrete examples in your documentation. Explained concrete cases. For example, how to automatically create search form based on post field, how to display form like it appears on backend, how could we massively import csv datas in ACF (as field id is not automatically generated on update), etc.

    Thanks.

  • Yes, when the page you are editing does not have field groups of it own then you need to add the field groups to $options. If you assigned the field groups to the “page” post type they would automatically be added.

    You have a field group that appears based on a selection in another field group? And this is working in the back end? As far as I know this can’t be done. You’ll need to explain this further.

    The rest of you comments, not sure where you’re going as these things don’t pertain to acf_form(). There isn’t a way to create a search form automatically and importing a csv file has nothing to do with acf_form(), or ACF for that matter. There is not import built into ACF.

  • “If you assigned the field groups to the “page” post type they would automatically be added”

    I have a CPT named “Document”. I have a ACF field group.
    On this group, I said :

    Show this field group if
    [Post type] [is equal to] [document]   -> it works
    or
    [Page] [is equal to] [my_page]         -> it doesn't work

    “You have a field group that appears based on a selection in another field group?”

    Yes. As conditional logic doesn’t work with taxonomy (very unfortunately despite many requests), I created another field group and I said :

    Show this field group if
    [Post taxonomy] [is equal to] [specific_choice_I_want]

    “specific_choice_I_want” is a value of a taxonomy field in my first field group.
    It works in backend (creating a new post in my CPT). It doesn’t work in frontend, even if I add field_group id to options.

  • I’m not sure why your front end form does not display when you set it to show on a specific page. I just tested this and it’s working for me. The only thing I can think of is that your page template does not have the acf_form() function call on it. Please check to make sure that all the needed code is included in your page template.

    The conditional logic works on the back end because the standard taxonomy meta box is there. Your field group is being displayed because a term is selected there and not because the term is selected in the “Taxonomy Field”. Conditional logic does not work across field groups and a term for the post or page is not set by making a choice in a taxonomy field. In order for the term of the post to be set you will need to save the post on the front end, after that the post will have a term selected.

  • Of course I have acf_form() !

    Here is the full code of this page :

    <?php acf_form_head(); ?>
    <?php get_header(); ?>
    
    </script>
    
    <div id="content">
    
    <div id="inner-content" class="wrap cf">
    
    <main id="main" class="m-all t-2of3 d-5of7 cf" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<article id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
    
    		<header class="article-header">
    
    			<h1 class="page-title" itemprop="headline"><?php the_title(); ?></h1>
    
    		</header> <?php // end article header ?>
    
    		<section class="entry-content cf" itemprop="articleBody">
    
    				<?php
    
    					$options = array(
    						'field_groups' => array('group_54dcecbb2f254','group_54ddd77bb7f6b'),
    						'post_id' => 'new_post',
    						'new_post'		=> array(
    							'post_type'		=> 'post',
    							'post_status'	=> 'pending',
    						),
    						'submit_value' => __('Add this document', 'xxx'),
    						'updated_message' => __('Document added.','xxx'),
    						'uploader' => 'basic',
    					);
    					acf_form($options);
    
    				?>
    
    		</section>
    
    	</article>
    
    <?php endwhile; endif; ?>
    
    </main>
    
    </div>
    
    </div>
    
    <?php get_footer(); ?>

    This code works because I specify field_groups ; without this value, nothing appears but the submit button.

    So, it’s totally impossible to display a group field (or, better, some fields into a group field) depending on a taxonomy field ? It’s very very awkward !

  • Maybe I’m not understanding. Is the problem that your form is not showing at all or that you need to include the groups you want to display in the options. The reason why the field groups will not appear without specifying them is that your field groups location rule is to display on the “Post” post type and you are displaying the form on a “Page”.

    Yes, you could have optional fields based on the taxonomy field. But the taxonomy field would need to be part of the same field group and the optional fields would need to be conditional fields based on the taxonomy field rather than making selections in the standard category meta box. It is not possible to display the standard taxonomy meta boxes on the front end of the site. This is a WP limitation, not and ACF limitation.

  • On my example, I specify field_groups because I have to in order to display it. If I don’t, I only have the submit button.

    The reason why the field groups will not appear without specifying them is that your field groups location rule is to display on the “Post” post type and you are displaying the form on a “Page”

    No ! My location rule is :
    Post type is equal to “document” —> OR <—
    Page is equal to “add document”

    And, of course, “Add document” is a page.

    Yes, you could have optional fields based on the taxonomy field

    What ?! Since when ? It doesn’t work for me !

    First field :
    “Type of document”,
    taxonomy,
    required,
    taxonomy : my own,
    appearance : radio buttons.

    Second field (only displayed if a specific taxo term is choosen) :
    “Keywords”,
    taxonomy,
    required : no,
    taxonomy : my own 2,
    conditional logic : yes -> Show this field if [No toggle fields available].

  • 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.

  • Thanks for this detailed answer !

    Many questions and remarks :

    – First of all, WHY I have to do all this, just because I use taxonomy? Why don’t you better integrate taxonomy in ACF(Pro)?

    – You said

    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

    but it works for me on backend (not in front).

    acf/save_post : it’s ok but as my taxo was not hierarchical, it didn’t really work. So, I don’t think it’s necessary to keep term_id : $term->name in field choices.
    Strangely, you use wp_set_post_terms and it works whereas codex said “For a taxonomy on a custom post type use wp_set_object_terms()”

    acf/pre_save_post : it’s ok. I would like to allow update post but I don’t try to do this for now.

    – …and I still don’t understand why I have to specify ‘field_groups’ for acf_form. What’s the aim of location rules if it doesn’t work ?

    Thanks.

  • – First of all, WHY I have to do all this, just because I use taxonomy? Why don’t you better integrate taxonomy in ACF(Pro)?

    Because ACF has some limitations, an these limitations are directly related to limitation in WP. ACF does not change the way WP works (even if it looks like it does) it only enhances what WP can already do and what is possible for anyone to do using core WP function if they wanted to do all the work themselves.

    – You said

    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

    but it works for me on backend (not in front).

    I seriously doubt that you have a ACF Field Group that only appears if a field in another ACF Field Group is a certain value unless you created the JavaScript to do it. You may have an ACF Field Group that appears if a certain term in the WordPress Taxonomy Meta Box is selected, but this is not the same as an ACF Taxonomy Field. The ACF Taxonomy Field and the WP Taxonomy Meta Box are two entirely different things that work in different ways that produce similar results. I don’t think that I can explain this any better or in any other way.

    – …and I still don’t understand why I have to specify ‘field_groups’ for acf_form. What’s the aim of location rules if it doesn’t work ?

    The reason that you need to do this is because you want to create new posts. If you wanted to show the current values of the current post or page and allow someone to update those values then yes, the location rules will work. But since your intention is to create a new post in a different post type than the post where the form is shown on the front end, setting the location of the field group to appear on both you template page where you want the form and on the post type where you want to create the new post, is pointless.

    you use wp_set_post_terms and it works whereas codex said “For a taxonomy on a custom post type use wp_set_object_terms()

    A post is a post is a post, even a post in a custom post type is just another post that has all of the same properties. Anything that works on the ‘post’ post type will work on a post in a custom post type.

  • To be clear, I only use ACF taxonomy field, with my own custom taxonomy. I don’t use WP meta box at all. But perhaps I don’t understand something.

    And so, I really have ACF Field Group that only appears if a ACF taxonomy field in another ACF Field Group is a certain value. But only in backend. I simply set Show this field group if [post taxonomy] [is equal to] [my_choice].

    Sincerely, I don’t understand why conditional logic is not possible with ACF taxonomy field. I don’t see why it’s related to WP core. It’s a select or a radio choice : if I select it, display field, if not, hide it. Why it’s so complicated ?

    Anyway, thanks for all your answers.

  • When you set

    [post taxonomy] [is equal to] [my_choice]

    You are using the built in Taxonomy in WP.

    [post taxonomy]

    is not an ACF Taxonomy field.

    The taxonomy field in ACF is not a choice field like a radio, checkbox or select. When you create a choice type field you specify what the values will be.

    The values that appear in a taxonomy field are loaded with Select2 which is a JavaScript input field add on. The values that will be displayed in the field are not known until they are displayed on the post to be selected. Well, this is just a theory. This may be a limitation of ACF or the Select2 script.

    When I want to create a field where the user can select something and that selection will be used as a condition on another field I use a standard select field, dynamically populated it, as in the code I gave previously, and then when the post is saved I do all the work myself of setting the taxonomy either with a acf/save_post or acf/pre_save_post filter.

  • Ok, it’s somehow strange for me. I thought it was ACF taxonomy field because it’s a field, in ACF, named Taxonomy. So what’s an ACF taxonomy field if it’s not that ?

    (for the conditional, it can’t be a select2 limitation because it works on backend, using select2)

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

The topic ‘understanding acf_form’ is closed to new replies.