There it is! Thank you!
This was a really obvious answer. I just deleted the array of hard coded categories and replaced it with an empty array. Categories automatically load fine now.
Here’s where I’m starting to build the form: make a list of checkboxes from previous answers and assign them to the $checkboxes[]
variable.
if ( $poll_responses->have_posts() ) :
while ( $poll_responses->have_posts() ) :
$poll_responses->the_post();
if ( have_rows( 'poll_answer', get_the_ID() ) ) :
while ( have_rows( 'poll_answer', get_the_ID() ) ) :
the_row();
// print checkboxes for each existing answer
echo '<input type="checkbox" name="checkboxes[]" ';
echo 'value="' . get_sub_field( 'answer', get_the_ID() ) . '">';
endwhile;
endif;
endwhile;
wp_reset_postdata();
endif;
// text or repeater field to add your own custom poll answer
// (this part works fine, values added as expected, show up in list of previous answers)
Then I save the checkbox answers and the new custom answers:
function dutchtown_save_poll_response( $post_id )
{
if ( get_post_type( $post_id ) !== 'poll_response' ) : return; endif;
if ( is_admin() ) : return; endif;
$post = get_post( $post_id );
if ( have_rows( 'poll_answer', $post_id ) ) :
while ( have_rows( 'poll_answer', $post_id ) ) :
the_row();
$answer[] = get_sub_field( 'answer', $post_id );
endwhile;
endif;
if ( ! empty( $_POST['checkboxes'] ) ) :
for ( $i = 0; $i < count( $_POST['checkboxes'] ); $i++ ) :
if ( $_POST['checkboxes'][$i] != '' ) :
add_row( 'poll_answer', array( 'answer' => $_POST['checkboxes'][$i] ) );
endif;
endfor;
endif;
}
The custom answers show correctly when the list is refreshed. The checkbox answers get inserted into the database, but show as blank both in the new checkbox list and when I go to look at the created post in wp-admin. I’m not sure where the disconnect is—or if I’m going about this all wrong.
I think I got it. Instead of
dutchtown_poll_form( $args ); // register form
acf_form( $poll_category ); // display form
I’ve set dutchtown_poll_form()
to just return the registration values:
$args = dutchtown_poll_form( $args ); // returns values rather than registering
acf_form( $args );
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.