I’ve created a drop down selection on a front end user profile which includes all custom post type posts.
On selecting it doesn’t actually save the selection, it just reverts back to the first option.
Where am I going wrong?
This is the code I have in my functions.php file:
add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) );
}
add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
$greeting = get_user_meta($profileuser->ID, 'teampage', true);
?><tr>
<th scope="row">Member of which Health Board?</th>
<td>
<select name="teampage" id="teampage" >
<?php $portfolioloop = new WP_Query( array(
'post_type' => 'board',
'post_status' => 'publish'
)); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<option id="Yes" <?php selected( $profileuser->teampage, 'Yes' ); ?>><?php echo the_title(); ?></option>
<?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
</select>
</td>
</tr><?php
}
I’m using this tutorial.
I found out what your problem is. It’s a couple of little mistakes that you made. Here is the working code:
You forgot to add the
valueattribute to eachoption.Also you shouldn’t check against the same value for all options(you used
selected( $profileuser->teampage, 'Yes' );, which essentially checks whether the value of$profileuser->teampageisYes). Instead we assign the post ID to each option and we check against that.