Skip to content

Commit 4d4a70f

Browse files
committed
gw-update-posts.php: Fixed issue where snippet returned ID instead of value when GPPA dynamically populates post field.
1 parent 00a1386 commit 4d4a70f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,26 @@ public function update_post_by_entry( $entry, $form ) {
140140
}
141141

142142
if ( $this->_args['terms'] ) {
143-
144143
// Assign custom taxonomies.
145144
$term_fields = is_array( $this->_args['terms'] ) ? $this->_args['terms'] : array( $this->_args['terms'] );
145+
146146
foreach ( $term_fields as $field ) {
147147
$term_field = GFAPI::get_field( $form, $field );
148-
$terms = array_map( 'intval', explode( ',', is_object( $term_field ) ? $term_field->get_value_export( $entry ) : '' ) );
149-
$taxonomy = is_object( $term_field ) ? $term_field['choices'][0]['object']->taxonomy : '';
150148

151-
wp_set_post_terms( $post->ID, $terms, $taxonomy );
149+
$terms = explode( ',', is_object( $term_field ) ? trim( $term_field->get_value_export( $entry ) ) : '' );
150+
151+
if ( ! empty( $terms ) ) {
152+
$taxonomy = str_replace( 'taxonomy_', '', rgars( $term_field, 'gppa-values-templates/value', '' ) );
153+
foreach ( $terms as $key => $term ) {
154+
// Check if `$term` is a term name or id. If term name, get the term id.
155+
if ( ! is_numeric( $term ) ) {
156+
$term = term_exists( $term, $taxonomy );
157+
$terms[ $key ] = $term ? $term['term_id'] : 0;
158+
}
159+
}
160+
161+
wp_set_post_terms( $post->ID, $terms, $taxonomy );
162+
}
152163
}
153164
}
154165

@@ -279,6 +290,16 @@ function acf_get_field_object_by_name( $field_name, $group_name = false ) {
279290
* @return mixed
280291
*/
281292
public function return_ids_instead_of_names( $value, $field, $template_name, $populate, $object, $object_type, $objects, $template ) {
293+
// Check if this is for the specific form we want.
294+
if ( $field->formId != $this->_args['form_id'] ) {
295+
return $value;
296+
}
297+
298+
// Don't want to return IDs for post objects used in the populates field dynamically using GPPA.
299+
if ( rgar( $field, 'gppa-values-enabled' ) === true && rgar( $field, 'gppa-values-object-type' ) === 'post' ) {
300+
return $value;
301+
}
302+
282303
if ( strpos( $template, 'taxonomy_' ) === 0 ) {
283304
$taxonomy = preg_replace( '/^taxonomy_/', '', $template );
284305
$terms = wp_get_post_terms( $object->ID, $taxonomy, array( 'fields' => 'ids' ) );

0 commit comments

Comments
 (0)