0

I've tried everything I can think of and read about. I cannot seem to figure out why this doesn't work, let alone get it to work. I have a Custom post type called Talk. That custom post type has a custom taxonomy (non-hierarchical) of Speaker. The taxonomy Speaker has three Advanced Custom Fields added to it: Institution, Phone, Email.

A Gravity Form using the Advanced Post Creation add-on creates a Talk as a draft. The speaker name, institution, phone and email are all fields on that form. Below is the latest version of my attempts to have the Speaker ACF update after post creation.

add_action( 'gform_advancedpostcreation_post_after_creation_2', 'after_post_creation', 10, 4 );
function after_post_creation( $post_id, $feed, $entry, $form ){
    $spkr_inst = rgar( $entry, '3' );
    $spkr_ph = rgar( $entry, '10' );
    $spkr_em = rgar( $entry, '11' );
    $talk_speakers = get_the_terms( $post_id, 'talk_speaker' );
    foreach($talk_speakers as $talk_speaker) {
        $spkr_id = 'speaker_'.$talk_speaker->term_id;
        GFCommon::send_email( '[email protected]', '[email protected]','','','New Post', $spkr_id);
        // update_field( 'speaker_institution', $spkr_inst, 'speaker_'.$talk_speaker->term_id);
        // update_field( 'speaker_phone_number', $spkr_ph, 'speaker_'.$talk_speaker->term_id);
        // update_field( 'speaker_email_address', $spkr_em, 'speaker_'.$talk_speaker->term_id);
    }
}

The commented out update_field stuff NEVER happens, whether I'm using 'speaker_'.$talk_speaker->term_id or the above $spkr_id. But the test email sends, and has the correct $spkr_id value in the email body, speaker_112 ...

I've read through these (but far be it from me to assume I missed nothing ...):

https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects

https://docs.gravityforms.com/gform_advancedpostcreation_post_after_creation/#examples

2 Answers 2

0

To resolve the issue with Gravity Forms and the Advanced Post Creation Add-On not updating ACF fields on a custom taxonomy, you can try adding the following code snippet to your WordPress theme's functions.php file:

add_action( 'gform_post_create_post', 'update_custom_taxonomy_acf_fields', 10, 3 );
function update_custom_taxonomy_acf_fields( $post_id, $form, $entry ) {
    // Check if the custom taxonomy term is set in the Gravity Form entry
    $custom_taxonomy_term = rgar( $entry, 'custom_taxonomy_field_id' );
    if ( ! $custom_taxonomy_term ) {
        return;
    }

    // Update the custom taxonomy term for the post
    wp_set_object_terms( $post_id, $custom_taxonomy_term, 'custom_taxonomy', false );

    // Update the ACF field for the custom taxonomy term
    update_field( 'acf_field_name', $custom_taxonomy_term, $post_id );
}

In this example, the code uses the gform_post_create_post action to hook into the Advanced Post Creation Add-On and update the custom taxonomy term and ACF field when a post is created. The code first checks if the custom taxonomy term is set in the Gravity Form entry, and if it is, it uses the wp_set_object_terms() function to update the custom taxonomy term for the post and the update_field() function from the Advanced Custom Fields plugin to update the ACF field.

Note: Make sure to replace "custom_taxonomy_field_id" with the actual field ID for the custom taxonomy field in your Gravity Form, "custom_taxonomy" with the actual name of your custom taxonomy, and "acf_field_name" with the actual name of your ACF field.

1
  • Thank you for weighing in. One piece of your code to note is the $post_id ... the value available upon GF form submission is not the value needed for the ACF update_field function. You would need to use taxonomy_function_name_N (where N is the term_id) in order to get the update to the ACF on the taxonomy term. See here: advancedcustomfields.com/resources/update_field/… In my code that's $spkr_id = 'talk_speaker_'.$talk_speaker->term_id; which I originally (and foolishly) prefixed with 'speaker_' ... once I realized this, it was quick work. Feb 7, 2023 at 19:14
0

Well. Thanks to this question: How to update custom taxonomy meta using ACF update_field() function or any other wordpress function

I slowed down long enough to realize I typed the function name of my own dang custom taxonomy incorrectly when prefixing the term id ... should have been 'talk_speaker_'.$talk_speaker->term_id; not just speaker_ ... in short, what I have works, if I didn't make that mistake.

Good night sleep and some coffee will do wonders.

Final Code:

add_action( 'gform_advancedpostcreation_post_after_creation_2', 'after_post_creation', 10, 4 );
function after_post_creation( $post_id, $feed, $entry, $form ){
    $spkr_inst = rgar( $entry, '3' );
    $spkr_ph = rgar( $entry, '10' );
    $spkr_em = rgar( $entry, '11' );
    $talk_speakers = get_the_terms( $post_id, 'talk_speaker' );
    foreach($talk_speakers as $talk_speaker) {
        $spkr_id = 'talk_speaker_'.$talk_speaker->term_id;
        update_field( 'speaker_institution', $spkr_inst, $spkr_id);
        update_field( 'speaker_phone_number', $spkr_ph, $spkr_id);
        update_field( 'speaker_email_address', $spkr_em, $spkr_id);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.