Get CPT post ID inside gform_form_post_get_meta [RESOLVED]

Hi everyone,

I’m trying to get the post ID at the very beginning of my function that is hooked using the “gform_form_post_get_meta” filter. Using the post ID, I need to get a value of a specific ACF field that is then used to get specific posts, and create checkbox fields using these posts. Everything work correctly expect of getting the ACF meta field, because the ID I’m receiving is incorrect - i’m receiving the ID of the first post inside the loop even though the get_the_id variable is defined before the loop.

I found out that removing the $form paramenter from my function returns the correct post_id, but then my function obviously does not work. So the $form parameter must be altering the loop in some way.

Could you please help me get the currently viewed post ID inside the gform_form_post_get_meta hook? Below is the function I’m using.

add_filter( 'gform_form_post_get_meta', 'suhlasy_vyzva' );
function suhlasy_vyzva( $form ) {
	//if ( get_post_type() == 'vyzvy' ) {
		global $post;
		$custom_id  = $form['id'];
		
		$pre_koho = get_field('urcena_pre', $custom_id );

		$suhlasy_query = new WP_Query(array(
			'post_type' => 'suhlasy',
			'meta_query' => array(
				array(
					'key'     => 'kategoria_suhlas',
					'value'   =>  $pre_koho,
					'compare' => 'LIKE',
				),
				array(
					'key'     => 'druh_suhlasu',
					'value'   =>  'vyzva',
					'compare' => 'LIKE',
				),
			),
			'posts_per_page' => -1
		));
		
		$souhlas_counter = 0;
		if ( $suhlasy_query->have_posts() ) {
			while ( $suhlasy_query->have_posts() ) {
				$suhlasy_query->the_post();

					$souhlas_counter++;

					$checkbox_text = get_field('checkbox_text');
					$text_suhlasu = get_field('text_suhlasu');

					$kategoria = get_field('kategoria_suhlas');

					$nazev_souhlasu = 'souhlas-field-' . $souhlas_counter; 
					$id_souhlasu = 1200 + $souhlas_counter;

					$nazev_souhlasu = GF_Fields::create( array(
						'type'   => 'consent',
						'id'     => $id_souhlasu, // The Field ID must be unique on the form
						'formId' => $form['id'],
						'isRequired' => true,
						'label'  => $nazev_souhlasu,
						'checkboxLabel' => $checkbox_text,
						'description' => '<h2>' . $custom_id . '-' . $kategoria . '</h2><br><strong>' . $pre_koho . '</strong>' . $text_suhlasu,
						'pageNumber'  => 1, // Ensure this is correct
					) );

					$form['fields'][] = $nazev_souhlasu;	
			}
		}
		wp_reset_postdata();
	//}
	return $form;
}

Hi You can use the following:

<?php
add_filter( 'gform_form_post_get_meta_1', 'suhlasy_vyzva' );
function suhlasy_vyzva( $form ) {

    foreach( $form['fields'] as $field )  {

  
        $field_id = 57;
        if ( $field->id != $field_id ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retreieved
        // more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
        $posts = get_posts( 'post_type= suhlasy&numberposts=-1&post_status=publish' );

        $input_id = 1;
        foreach( $posts as $post ) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }

            $choices[] = array( 'text' => $post->post_title, 'value' => $post->ID );
            $inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );


            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;
    }
    return $form;
}

add_filter( 'gform_field_choice_markup_pre_render_1_57', 'suhlasy_fields', 10, 4 );
function suhlasy_fields( $choice_markup, $choice, $field, $value ) {
    $posts = get_posts( 'post_type=suhlasy&numberposts=-1&post_status=publish' );

    foreach ( $posts as $post ) {
        $getCheckboxText  = get_field('checkbox_text', $post->ID);
        $getTextSuhlasu   = get_field('text_suhlasu', $post->ID);
        $getKategoria     = get_field('kategoria_suhlas', $post->ID);
        
        if ($field->get_input_type() == 'radio' && rgar( $choice, 'value' ) == $post->ID  ) {
            $checkboxText = '<div class="checkbox_text">'. $post->checkbox_text .'</div>';
            $textSuhlasu  = '<span class="text_suhlasu">'. $post->text_suhlasu .'</span>';
            $kategoria    = '<div class="kategoria">'.$post->kategoria.'</span>';
            return str_replace( "</label>", " $checkboxText $textSuhlasu $kategoria</label>", $choice_markup );
        }
    }

    return $choice_markup;
}

For Marian, the issue was a conflict with another plugin (one of the non-official plugins for Polylang integration) that caused the WordPress function to return null instead of a post ID. Resolved by the customer on Facebook and support ticket.