How to add a metadata in a checkbox choice option in gravityForm and call the Post_id dynamically?

24 Views Asked by At

Hello all thanks in advance for your help. I have two ACF fields for hotel room, one is single occupancy, the other one is double occupancy, the meta name is Lodging_0_price_single_occupancy and Lodging_0_price_double_occupancy. then I have a gravity form (GF) and a checkbox field where I would like to display this two metavalues as choices (label and value are needed) in the same checkbox field.

What i did: i created a checkbox field in GF and used populate anything add-on to display the Lodging_0_price_single_occupancy (text: Single occupancy; value :Lodging_0_price_single_occupancy) then to add the other choices double occupancy, i wanted to use this code

add_filter( 'gppa_input_choices_24_6', function( $choices, $field, $objects ) {

    array_unshift( $choices, array(
        'text'       => 'new choice',
        'value'      => 'new value',
        'isSelected' => false,
    ) );

    return $choices;
}, 10, 3 );

using that code above it does add new choice and new value to the checkbox choices in GF, but i would like to use the metadata: Lodging_0_price_double_occupancy instead.

Any idea how can i do something like that?

I found something:

add_filter( 'gppa_input_choices_24_6', function( $choices, $field, $objects ) {
    
    $valueNew = get_metadata( 'post', 4310,'lodging_room_0_room_info_price_price_cp_double_occupancy', true ); // working but you have to type the post id -- not dynamically
    
    array_unshift( $choices, array(
        'text'       => $valueNew,
        'value'      => $valueNew,
        'isSelected' => false,
    ) );
    

    return $choices;
}, 10, 3 );

It is working but i have to put manually the post_id (here 4310).

Do you know how to call the post_ID dynamically? if i run a var_dump($objects); then i will get this:

array(1) { [0]=> object(WP_Post)#12912 (24) { ["ID"]=> int(4310) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2023-05-09 20:24:50" ["post_date_gmt"]=> string(19) "2023-05-09 14:54:50" ["post_content"]=> string(0) "" ["post_title"]=> string(15) "Hotel HEIGA vns" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(15) "hotel-heiga-vns" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2023-10-30 08:26:27" ["post_modified_gmt"]=> string(19) "2023-10-30 02:56:27" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(58) "https://netrekandtravel.com/?post_type=lodging&p=4310" ["menu_order"]=> int(0) ["post_type"]=> string(7) "lodging" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }

Can you tell me how to access to the ID 4310 in the array ? thanks again

Edit: It seems that i have to run $desired_id = $objects[0]->ID; and then use that in the add_filter

add_filter( 'gppa_input_choices_24_6', function( $choices, $field, $objects ) {

    $desired_id = $objects[0]->ID;
    $valueNew = get_metadata( 'post', $desired_id ,'lodging_room_0_room_info_price_price_cp_double_occupancy', true ); 

    array_unshift( $choices, array(
        'text'       => $valueNew,
        'value'      => $valueNew,
        'isSelected' => false,
    ) );


    return $choices;
}, 10, 3 );

The result shows up. But am i doing it right?

0

There are 0 best solutions below