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;
}