0
votes

I have created a custom post type 'Candidates' which will be used to store the info submitted by a candidate who is applying for a vacancy. I also have a custom post type called Vacancies.

I have used ACF for the custom fields on both sets of post types.

I have setup an ACF front-end form to allow candidates to submit applications. I have a button that user clicks on the vacancy that sends them to a page where the form is. As part of the button I am passing the title of the vacancy and am able to pass and echo it on the page where the form is (so it is passing through).

for the life of me I cant get this vacancy title field to auto-populate the field on the front-end form.

Here are the code snippets. Page where I am creating the front-end form

$vacancy = isset( $_GET['vacancy'] ) ? esc_attr( $_GET['vacancy'] ) : '';
$vacancyurl = isset( $_GET['vacancyurl'] ) ? esc_attr( $_GET['vacancyurl'] ) : '';

acf_form_head();
get_header();

?>



    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <div class="candidateformcontainer">

                <?php /* The loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>
                    <div>
                       <h1><?php echo get_the_title(); ?></h1>
                        <p>To proceed with submitting your application for <strong><?php echo $vacancy; ?></strong> complete the application form below.</p>
                    </div>
                    <?php 

                    acf_form('acf-candidate-application-form'); 

                    ?>

                <?php endwhile; ?>               

            </div>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_footer();

The field I am trying to pass the title to (so the $vacancy variable)

<?php the_field( 'vacancy_applied_for' ); ?>
Field Number: field_5dba152669aaf

Any help or pointers would be much appreciated. I have exhausted the ACF documentation.

1

1 Answers

1
votes

So thanks to the amazing guys from ACF managed to get an answer to this issue.

You would make use of the Prepare Field filter to update the field before with your passed in value prior to it rendering on the page.

Here is the link to the help doc: https://www.advancedcustomfields.com/resources/acf-prepare_field/

Using this filter I needed to add the following snippet into my functions.php file.

    function populate_vacancy_applied_for($field) {
  // only on front end
  if (is_admin()) {
    return $field;
  }
  if (isset($_GET['vacancy'])) {
    $field['value'] = $_GET['vacancy'];
  }
  return $field;
}

add_filter('acf/prepare_field/key=field_5dba152669aaf', 'populate_vacancy_applied_for');