Skip to content

Commit be97d6f

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 be97d6f

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,34 @@ 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+
// Get the taxonomy from the field settings or the first choice object based on the field type.
153+
if ( $term_field instanceof GF_Field_Text ) {
154+
$taxonomy = str_replace( 'taxonomy_', '', rgars( $term_field, 'gppa-values-templates/value', '' ) );
155+
} elseif ( is_object( $term_field ) && ! empty( $term_field['choices'][0]['object']->taxonomy ) ) {
156+
$taxonomy = $term_field['choices'][0]['object']->taxonomy;
157+
} else {
158+
$taxonomy = '';
159+
}
160+
161+
foreach ( $terms as $key => $term ) {
162+
// Check if `$term` is a term name or id. If term name, get the term id.
163+
if ( ! is_numeric( $term ) ) {
164+
$term = term_exists( $term, $taxonomy );
165+
$terms[ $key ] = $term ? $term['term_id'] : 0;
166+
}
167+
}
168+
169+
wp_set_post_terms( $post->ID, $terms, $taxonomy );
170+
}
152171
}
153172
}
154173

@@ -279,6 +298,16 @@ function acf_get_field_object_by_name( $field_name, $group_name = false ) {
279298
* @return mixed
280299
*/
281300
public function return_ids_instead_of_names( $value, $field, $template_name, $populate, $object, $object_type, $objects, $template ) {
301+
// Check if this is for the specific form we want.
302+
if ( $field->formId != $this->_args['form_id'] ) {
303+
return $value;
304+
}
305+
306+
// Don't want to return IDs for post objects used in the populates field dynamically using GPPA.
307+
if ( rgar( $field, 'gppa-values-enabled' ) === true && rgar( $field, 'gppa-values-object-type' ) === 'post' ) {
308+
return $value;
309+
}
310+
282311
if ( strpos( $template, 'taxonomy_' ) === 0 ) {
283312
$taxonomy = preg_replace( '/^taxonomy_/', '', $template );
284313
$terms = wp_get_post_terms( $object->ID, $taxonomy, array( 'fields' => 'ids' ) );

0 commit comments

Comments
 (0)