Home › Forums › General Issues › Choose ACF relationship field at registration form
So I have an ACF relationship that I’ve made called related_study_paths
for my website. I filter it using a custom post type study_path
and there are 3 study path posts. In the locations tab, I have tried linking the relationship to the Custom User Roles ‘student’ and ‘faculty’. And an input field shows in the user profile in the wp-dashboard, where I can choose a study path and save it.
Now I want the user to choose a related study path during the registration process. I have made a custom registration form so, how do display an input field where a user can choose one of the 3 study paths.
Because the 3 study paths are basically posts under the study_path post type, I imagine you can build a dropdown/radio selection and manually add the options by grabbing the 3 study path’s titles and post ids —or— you can create a custom WP query to grab all posts under the study_path post type if you plan to add more posts down the road.
Hi, yea I have tried that and it works in displaying the dropdown, but the saving doesn’t happen in wp_usermeta.
I added the ACF form location to the default WordPress Register page and it works, it saves the related study path for the default ‘student’ user. I just want the same form on my custom registration form because I am not using the default WordPress registration.
To add ACF fields to your custom registration form you would have to use acf_form() with the “form” argument set to false and you would call acf_form() between the <form></form>
tags of your other form. Of course this means that you are able to call a PHP function in the correct location.
If you cannot do the above then you would need to create your own input field and then you would need to use PHP to update the fields yourself when it is submitted using update_field().
i have put this code inside the form tag
<p class="input_container">
<label for="related_study_paths"><?php _e('Related Study Paths'); ?></label>
<?php
// Retrieve the current user's selected study paths
$selected_study_paths = get_field('related_study_paths', 'user_' . get_current_user_id());
// Render the ACF Relationship field choices
acf_form(array(
'post_id' => 'user_' . get_current_user_id(),
'post_title' => false,
'form' => false,
'field_groups' => array('group_XXXXXXXXX'), // Replace with your field group key
'fields' => array('related_study_paths'), // Replace with your field name
'return' => false,
'submit_value' => false,
'values' => array('related_study_paths' => $selected_study_paths), // Pass the selected choices
));
if ($selected_study_paths) {
echo '<ul>';
foreach ($selected_study_paths as $study_path) {
echo '<li>' . get_the_title($study_path) . '</li>';
}
echo '</ul>';
}
?>
</p>
It does output a form field with a search bar but it does not have anything inside it to select from.
After looking into this further, you are not going to be able to use acf_form(). This issue is that this is a user registration form. There is no value get_current_user_id()
under this condition. Also, ACF will not know that a new user is being created and will not be able to get the correct user id to save the fields for.
What you will have to do instead is create a select field that you populate on your own with the select values. Since this you are saving a value in some type of relationship field the values need to be the ID of the related post.
Then you will need to create an action for the WP user_register hook. This will provide the user ID just created. Then using this ID you use update_field(), using the field key and the "user_{$user_id}"
post ID.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.