• Resolved kisabelle

    (@kisabelle)


    I have a form in which I’m creating a new post using the af/form/submission/key=[form_key] action.

    The form contains Taxonomy fields for multiple custom taxonomies – Document Type (select), Document Category (checkbox) and Document Tag (multi-select). All three are set to return Term ID.

    For Document Type and Document Category I have disabled “Create New Terms” on the field, and they are saving correctly. However for the Document Tags I have enabled “Create New Terms” so that users can add new tags. This one is not saving correctly. When an existing tag is selected, for example “parking” which has an ID of 334, instead of assigning that tag, a new tag is created with the name “334” and slug “334”.

    I think it is most likely caused by the “Create New Terms” option.

    My code is as follows:

    $doc_type = af_get_field('type');
      $doc_cat = af_get_field('document_category');
      $doc_tag = af_get_field('document_tags');
      $custom_tax = array(
        'document-type' => $doc_type,
        'document-category' => $doc_cat,
        'document-tag' => $doc_tag
      );
        
      // Set initial post data from form fields
      $post_data = array(
        'post_type'     => 'document',
        'post_status'   => 'draft',
        'post_title'    => $document_title,
        'post_content'  => '',
        'tax_input'     => $custom_tax
      );
    
      // Create post
      $post_id = wp_insert_post( $post_data );

    Please advise.

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author fabianlindfors

    (@fabianlindfors)

    Hi!

    I think I know what’s happening here. By default your taxonomy fields will return term IDs for the selected tags. The problem is that the ‘tax_input’ is not expecting term IDs but term names, and when it encounters these IDs it doesn’t find any with matching names and as such creates new ones.

    The way I would solve this is by using ACFs built-in support for saving taxonomies. On your taxonomy fields there should be an option “Save terms”, check that. Once that’s set up you can simply save your taxonomy fields to your post and ACF should take care of the rest. My plugin has a helper plugin for this called “af_save_field”. Your code may look something like this instead:

    
    // Set initial post data from form fields
    $post_data = array(
      'post_type'     => 'document',
      'post_status'   => 'draft',
      'post_title'    => $document_title,
      'post_content'  => '',
    );
    
    // Create post
    $post_id = wp_insert_post( $post_data );
    
    // Save taxonomies
    af_save_field( 'type', $post_id );
    af_save_field( 'document_category', $post_id );
    af_save_field( 'document_tags', $post_id );
    

    Hope this helps!

    Thread Starter kisabelle

    (@kisabelle)

    Thank you for the info Fabian. I see now that wp_insert_post ‘tax_input’ expects taxonomy names – interesting that it was somehow saving the other taxonomies correctly when I was feeding it taxonomy IDs the whole time haha.

    I was using ‘af_save_field( ‘field_name’, $post_id );’ already for additional custom fields, not sure why it didn’t occur to me to try using that for saving the taxonomy fields. Worked perfectly!

    Thanks again for your help 🙂

    • This reply was modified 5 years, 8 months ago by kisabelle.
    • This reply was modified 5 years, 8 months ago by kisabelle.
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Wordpress can be a curious beast sometimes!

    I’m glad this worked for you, feel free to reach out if you run into any other issue! 🙂

    Also if I may I’d like to recommend Advanced Forms Pro which has built-in support for post editing as well as direct, priority support. Seems like you’re just fine with the free version but thought I’d tell you about it anyways!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue Assigning Custom Terms to New Post’ is closed to new replies.