I’m creating a custom backend in WordPress, and one part of it is to be able to select from a list of case studies that is automatically updated with a foreach loop, and insert a link to the selected case study on the page. The radio list in the backend is created using the following code
<?php
$checked = ' checked="checked"';
$mypostype = get_posts(array(
'post_type' => 'case_study',
'showposts' => -1));
?>
<?php $metabox->the_field('case-study-link'); ?>
<p id="case-study-select">
<label>Which case study goes here?</label>
<?php foreach ( $mypostype as $mypost ) { ?>
<input type="radio" name="<?php $metabox->the_name(); ?>" value="<?php echo get_permalink_by_name( $mypost->post_name ); ?>" <?php echo $checked; ?>>
<?php echo $mypost->post_title; ?>
<?php } ?>
</p>
The problem with using inside the foreach loop is obviously that every radio button is seen as selected, so the last one in the list is always the one marked. How can I modify this to make sure the button that was truly selected is marked as such?
Note: the functionality works as intended – when you select a case study, the correct link is inserted into the page, but in the backend the wrong item is shown as selected.
By default first option will be selected, but the values will be changed according to selected radio button value