0

I've got a CPT for random homepage banner images (so we can test different images and call to action language). I've got a custom field for the post type that accepts a specific ID for each banner so we can track it. I need to then get that ID into Gravity Forms.

Here's where the issue I'm having is coming in (and it's not Gravity Forms specific). I'm running a query in my function but it's giving me random ID's in Gforms instead of the actual ID of the banner at the time the form is filled out (which happens to be in a modal window). I'm using ACF to create the custom fields if that makes a difference (there is ACF specific code in my function).

Here's my function:

add_filter('gform_field_value_Banner_ID', 'populate_Banner_ID');

function populate_Banner_ID($value){
    global $wp_query;
    $args = array (
        'post_type' => 'home_banners'
    );
    $home_banners = new WP_Query( $args );
    while ( $home_banners->have_posts() ) : $home_banners->the_post();
    $banner_id = get_field('home_banner_mail_id', $post->ID);
    endwhile;
    return $banner_id;
}

I've tried global $post; too and that didn't work. Like I said, it's grabbing the custom field and populating Gravity Forms with it, but it seems to be grabbing a random version of the ID.

I've used the following on a page that doesn't load the form in a modal and it works as expected (and the ID on this page is dynamic too). I also tried this for the specific form I'm using in the modal, but it doesn't pick up the ID at all.

function populate_ppc_mail_id($value){
    global $post;

    $mail_id = get_field('ppc_mail_id', $post->ID);

    return $mail_id;
}

add_filter('gform_field_value_mail_id', 'populate_ppc_mail_id');

Can someone point to what I'm doing wrong in my query that it's not picking up the correct ID? I've got the id loading as a css ID to test that it works, and that part is working as expected--it's just this specific query it seems to not want to cooperate.

Thanks in advance!

0

The $post variable inside your function does not point to anything. You are declaring global $wp_query (which you are not actually using), but not global $post.

  • I've tried global $post; and I get the same results, which are not the results I'm looking for. It's not actually capturing the ID of the banner currently on the page--that's the problem I'm having. – Justice Is Cheap Apr 27 '15 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.