Support

Account

Home Forums ACF PRO Creating a custom term and updating the field data with update_field

Solved

Creating a custom term and updating the field data with update_field

  • So I’ve been working on some new functionality for a client whereby they want a frontend form that can be submitted to create a new custom taxonomy and populate the custom fields within it.

    I have a form setup that uses wp_insert_term to create a taxonomy which all works fine. I then get the new taxonomies ID, and append the given data to the custom field using update_field like so:

    <?php update_field( 'field_5b2839d326a1c', $fieldValue, $term_id ); ?>

    But nothing seems to be coming through. I’ve tried using the field name instead of the key, I’ve done an output on the $fieldValue and the $term_id just before running the update_field and they both have values (and the term_id is the same as the post that I am trying to update).

    Where would be the next logical step in debugging this? I’m at a bit of a loose end. If it helps here is my current function (I’ve stripped some of the namings out to keep my client unidentifiable, but the logic is all the same).

    <?php
    function create_new_artist( $entry, $form ) {
    	// Pass form data to varibles
    	$taxonomy_title		= rgar( $entry, '2' );
    	$field_data 		= rgar( $entry, '3' );
    
    	// Debugging
    	print_r( 'Title => ' . $taxonomy_title . '</br> Field data => ' . $field_data . '</br>');
    
    	// Adding the term
    	$new_taxonomy = wp_insert_term( 
    		$taxonomy_title, 
    		'product_cat'
    	);
    
    	// Check the new term was added without any errors
    	if (!is_wp_error($new_taxonomy)) {
    		// Get the new term ID 
    		$term = get_term_by('name', $taxonomy_title, 'product_cat');
    		$term_id = $term->term_id;
    
    		print_r($term_id);
    
    		// Add custom field data to new term
    		update_field( 'field_5b2839d326a1c', $field_data, $term_id );
    	} else {
    		GFCommon::log_error($new_artist->get_error_message());
    	}
    
    } add_action( 'gform_after_submission_7', 'create_new_artist', 10, 2 );
  • the post ID for update field for a term should be 'term_'.$term_id

  • How embarrassing!

    Thanks, John. I shouldn’t make that mistake twice!

  • It’s an easy mistake and these types of things are always more visible to others.

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

You must be logged in to reply to this topic.