Dynamically populate not working

I am trying to dynamically populate a standard text field with values from a custom taxonomy. The correct result shows when I echo it but it doesn’t return to the field. Any ideas?

add_filter( ‘gform_field_value_jobs’, ‘populate_jobs’ );
function populate_jobs( $value, $field, $name ) {

$terms = wp_get_post_terms( $post->ID , array( 'cg_jobs_job_type') );

foreach ( $terms as $term ) {
    $result = $term->name;
}
    return $result;

}

In your text field, do you just want all the values from the taxonomy in one string? Like

electrician barber postman roofer programmer

?

Can you explain what you want the end result to look like?

I only need it to show the first value of the taxonomy. Ideally the creator of the post will only select one job type which is the name of the taxonomy. Right now when I echo $result it show up on a page but when I try to return it to the field nothing shows up. I can change the “return $result;” to "return (‘test’); and the string test shows up so I know data is getting to the field. I just don’t know why the value of $result is not showing in the field.

Can you please share all your code: the function and the add_filter line? Thank you

add_filter( 'gform_field_value_jobs', 'populate_jobs' );
function populate_jobs( $value ) {
    
$terms = wp_get_post_terms( $post->ID , array( 'cg_jobs_job_type') );

foreach ( $terms as $term ) {
    $result = $term->name;
}
    return $result;

}

Thank you for the code. Can you try it with some additional logging like this:

<?php
add_filter( 'gform_field_value_jobs', 'populate_jobs' );
function populate_jobs( $value ) {
	GFCommon::log_debug( __METHOD__ . '(): Post => ' . print_r( $post, true ) );
	$terms = wp_get_post_terms( $post->ID , array( 'cg_jobs_job_type') );
	GFCommon::log_debug( __METHOD__ . '(): Post Terms => ' . print_r( $terms, true ) );
	foreach ( $terms as $term ) {
		GFCommon::log_debug( __METHOD__ . "(): Setting result to term name {$term->name}." );
		$result = $term->name;
	}
	GFCommon::log_debug( __METHOD__ . "(): Returning this result {$result}." );
	return $result;
}

That will only work if you also enable logging. First enable logging on the Forms > Setting page. Set the Logging radio button to “On”. Then go to Forms > Settings > Logging and ensure that logging for Gravity Forms Core is set to “log all messages”. After testing the form with the new logging statements added to your code, and logging enabled, refer back to the core Gravity Forms log on that same page (Forms > Settings > Logging). Click the view log link.

{CH edited out the quoted code}

Ok I did that

OK, thank you. On the Forms > Settings > Logging page, can you copy and share the link to the Gravity Forms Core log? If you don’t want to share that log here, please send me a private message with the link.

http://webproofingsite.com/centurygroupdev/wp-content/uploads/gravity_forms/logs/gravityforms_fce0b66f22be4fe809d61433de4af8aecf0f1855.txt

If you search for any of those logging statements (like “Returning this result”) in the log, they do not appear. Did you test the form? If so, are you using the correct parameter name jobs in the form builder for that field?

I didn’t find any of those terms so I changed the return $result to return (‘test’) and ran it again and now see the terms that you are talking about. When I changed the code back it didn’t log anything.

If you look in the log, you’ll see the $post is empty, and therefore post term are empty and nothing is returned.

Do you need a global $post; at the beginning since you’re not in the Loop?

Where would I add that. This code I am working on is in my functions.php file. Just add it before the code?

This form that I am using is embedded as a popup on a page.

I am wondering if the popup is my problem and how I might get the current post values. I don’t know if this makes sense.

I added the global in the function and it works. Thanks for your help I really appreciate it!!

1 Like

I’m glad you figured it out! Can you post the complete code here for future reference?