Support

Account

Home Forums Front-end Issues create new taxonomy with acf form not new post

Solved

create new taxonomy with acf form not new post

  • Hi i want to save a new taxonomy, but i cant get the form to frontend.
    Custom field are sucess visible in backend but on frontend i cant see, i think becuse i dont have right code. My custom taxonomy name is “narocilnice” and code is:

    <?php acf_form(array(
        'id' => 'acf-form',
        'post_id' => true,
        'post_title'	=> true,
        'new_post'		=> array(
            'tax_name'		=> 'narocilnice',
        ),
        'submit_value'		=> 'Shrani',
        'html_submit_button'	=> '<input type="submit" class="acf-button btn btn-primary" value="%s" />',
        'uploader' => 'wp'
        )); 
    ?>

    On this picture printscreen you can see that i get on backend custom field but on frontend i can’t. So i need add new taxonomy to custom taxonomy name “narocilnice” not new post to custom post_type.

    Can someone tell me what is wrong?

  • To to this you will need to

    1) set the “new_post” argument to acf_form() something unique, like “new_narocilnice”
    2) Supply the field groups or fields you want to display setting either the “field_groups” or “fields” arguments
    3) Create an acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/ that looks for your unique “new_post” value
    4) Create the term in the your filter
    5) The return post id from your filter needs to be “term_{$term_id}”

  • @john Huebner thanks for answer. But i dont understand exacly what i must do. Do you have some example code for this. Then i can do if i saw some exmpale code. For now i have on custom template this:

    acf_form(array(
        'id' => 'acf-form',
        'post_id' => 'new_post',
        'post_title'	=> true,
        'new_post'		=> array(
            'post_title'		=> 'new_narocilnice',
        ),

    and also i have already filter function my_pre_save_post( $post_id ) { .... but i dont know what i must check in filter and how then save all custom field.

  • I do not know of any examples.

    
    acf_form(array(
      'post__id' => 'new_narocilnice',
      'field_groups' => array(
        'group_XXXXXXX'
      )
    ));
    
    
    function new_narocilnice_filter($post_id) {
      if ($post_id != 'new_narocilnice') {
        return $post_id;
      }
      // get values for new term from $_POST['acf'] values
      // https://developer.wordpress.org/reference/functions/wp_insert_term/
      $term = insert_term($term_name, $taxonomy, $other_args);
      $post_id = 'term_'.$term=>term_id;
      return $post_id;
    }
    
  • Hi,

    yes this work but now i have next problem.

    My structure is like next example:

    Custom Post Type: Pregledi
    – Custom taxnomy: Lokacija
    – Custom taxnomy: Naročilnice

    Now on acf form naročilnice i also have Taxonomy field coneected to “Lokacija”. So if i explain i assing custom taxonomy Lokacija to taxonomy Naročilnice. And when i do this over backend it work if i asign from frontend i cant that it works.

  • I’m don’t understand your second issue.

  • I solve this. Except one problem. Now i’m using next code for form

    <?php acf_form(array(
    				'post_id' => 'nova_narocilnica',
    				'field_groups' => array(
    					'group_5f475552ca375'
    				 ),
    				'post_title'	=> true,

    and working well and then with pre_save filter i add manualy this acf filed to database (i cerate new term to custom taxonomy). But problem is when i refresh this page where i have this form included i get in inputs the latest saved values. But i save this values for custom taxonomy not to this page, so why then i get latest saved input values.

    When i go researching i find that if i change 'post_id' => 'nova_narocilnica', change to something else the input are blank but still when i submit form it also save to this page. why?

  • Under Locations, select the Taxonomy Term rule and choose the corresponding value to show this field group.

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

You must be logged in to reply to this topic.