Skip to content Skip to sidebar Skip to footer

How To Get A Dynamic Dropdown With An ACF Relationship Field

My website lists courses and course providers. I'm using ACF relationship to assign the providers to each course. both courses and course providers are custom post types I have a c

Solution 1:

To make things easier you could add the email address as a data attribute to the tag for each provider title like so:

<select name="supplier" id="supplier" class="form-control">
                    <option value="">---</option>
                    <?php
                    $posts = get_field('course_providers');
                    if( $posts ): ?>
                        <?php foreach( $posts as $post): ?>
                            <?php setup_postdata($post); ?>
                            <option value="<?php the_title(); ?>" data-email="<?php the_field('email_address'); ?>"><?php the_title(); ?></option>
                        <?php endforeach; ?>
                        <?php wp_reset_postdata(); ?>
                    <?php endif; ?>
                </select>

Then in your jQuery you can do something like:

$(document).ready(function() {
    $('select#supplier').change(function() {
        var emailAddress = $('select#supplier').find(':selected').data('email');
    });
    // Do something with the var emailAddress on form submit here
});

Post a Comment for "How To Get A Dynamic Dropdown With An ACF Relationship Field"