multiple checkbox custom fields from Gravity Forms to ACF

524 views
Skip to first unread message

barbara schendel

unread,
Jul 24, 2021, 1:39:49 PM7/24/21
to Minneapolis St. Paul WordPress User Group
Hi all, does anyone have experience collecting the values of checkbox custom fields from gravity forms to ACF?

We have a custom post type called Markets with multiple custom fields. We are using a front end gravity from to let users submit a new market post to the map. I'm having trouble with some checkbox fields. 

In ACF I have a custom field called Amenities which is a checkbox field. It has several options like so:
freepark : Free Parking
paypark : Pay to Park
metro : Metro Route
ada : ADA Access
restrooms : Restrooms
 (and so forth)
acf-checkboxes.jpg


In Gravity Forms, we are creating a custom post type from each form submission. I have a "custom field" field, with field type set to "Checkboxes" and have entered each of the above amenities with label and value set to match the ACF values exactly, and mapped the whole thing to my existing ACF field.
gf-checkboxes.jpg


However, when the form is submitted, rather than saving an array of checkbox values to ACF, only one (the first one) of the values is saved. 
acf-market.jpg

How do you recommend connecting these to each other? Do I need to add something to functions.php? A google search revealed others have had related issues, but all slightly different so that their code examples were not quite relevant to my situation. 

Appreciate any advice, thank you!

Nick Ciske

unread,
Jul 24, 2021, 3:54:44 PM7/24/21
to Mpls-Stpaul-Wordpress
Is there a reason you’re not using an ACF frontend form for this? That would solve this issue, but may introduce others. 

I believe GF and ACF save multi checkboxes differently so you’ll need to add a pre save filter to the meta field(s) to transform the data into the correct format before it’s written to the DB.

I don’t know the format off the top of my head, but you should be able to save the same values in acf and gf and compare the formats to see what conversion needs to happen.

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpls-stpaul-wordpress/d4f145f8-5a18-4d34-8297-d148f7fc6e8an%40googlegroups.com.

barbara schendel

unread,
Jul 24, 2021, 4:21:19 PM7/24/21
to mpls-stpau...@googlegroups.com
I am using that for editing the post after it's created but I am using GF to create it because the client has a lot of specific wishes for the appearance of the front end form which was much more expedient to build in GF. 

Does this code look like what one might want to use? It was posted by someone else but I haven't ever done this before so I couldn't follow it.

add_action('gform_pre_submission', 'wpq_register_added_meta_hook');
add_action('gform_post_submission', 'wpq_remove_added_meta_hook');
function wpq_register_added_meta_hook()
{
add_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}
function wpq_remove_added_meta_hook()
{
remove_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}
function wpq_added_post_meta($meta_id, $object_id, $meta_key, $meta_value)
{
global $wpdb;
if(!class_exists('Acf'))
return;
$query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}acf_fields WHERE name = %s", $meta_key);
$acf_field = $wpdb->get_row($query);

if($acf_field)
{
$query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}acf_values (field_id, value, post_id) VALUES (%d, %d, %d)", $acf_field->id, $meta_id, $object_id);
$wpdb->query($query);
}
}



Aaron Silver

unread,
Jul 26, 2021, 2:25:35 PM7/26/21
to mpls-stpau...@googlegroups.com
Barbara,

I think that code isn't quite what you need; it has to look at the just-submitted GF and save the info to ACF fields. I don't really think there should be any raw SQL.

PM me if you want; I can probably help you get this solved programmatically.

Aaron.
Reply all
Reply to author
Forward
0 new messages