Hello, I’ve got a form where users can post reviews on courses, this generates new “reviews” custom posts. I would like to save the course ID with each review, so that I can later do a query and output reviews related to a specific course.
Is it possible to add a hidden field, pre-populate it with the course ID and save it along with all other review data?
Alternatively, is it possible to have two field groups (both connected to my custom post type of course), one which I use for the form and one that’s just for the admin, but have the form post to a custom field in this admin-only group?
I.e. I would have one group with Name, Country, Review etc, which makes up the form, and one group in the admin with course ID and other fields that are admin-only – and have the form pass data (like course ID) to this second group?
I’m using WP 4 and ACF 5 PRO, so the form is in a page template rather than in functions.php and looks like this:
<?php acf_form(array(
'post_id' => 'new_post',
'field_groups' => array( 2516 ),
'new_post' => array(
'post_type' => 'reviews',
'post_status' => 'draft',
'post_title' => $_POST['acf']['field_54495d4dd9129']
),
'submit_value' => 'Create a new review',
'updated_message' => 'Thank you for the review!'
)); ?>
Ps. I’m also trying to pass the name-field data (field_54495d4dd9129) to the review post title, but that doesn’t seem to work, even though the specific field has that value if I inspect the form element. Is something wrong with the code above?
Got this working. For the hidden field issue, we used a field from a different field group (that isn’t part of the frontend form) and used the html_after_fields function:
<?php acf_form(array(
'post_id' => 'new_post',
'field_groups' => array( GROUP-ID ),
'new_post' => array(
'post_type' => 'reviews',
'post_status' => 'draft',
),
'submit_value' => 'Create a new review',
'updated_message' => 'Thank you for the review!',
'html_after_fields' => '<input type="hidden" name="acf[field_KEY-FROM-ADMIN]" value="OUR-POST-VALUE"/>',
)); ?>
For the other issue with post_title, it seems to have been an issue with this form value (text field) not being available when the form saved the new post the standard way. So in functions.php, we have this:
<?php
function acf_review_before_save_post($post_id) {
if (empty($_POST['acf']))
return;
$_POST['acf']['_post_title'] = $_POST['acf']['field_KEY-FROM-ADMIN'];
return $post_id;
}
add_action('acf/pre_save_post', 'acf_review_before_save_post', -1); ?>
The topic ‘acf_form hidden field’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.