Hi Jim,
Thank you for contacting us and I'd be happy to assist.
For what you're planning to achieve, it is important that at least 1 custom field value for the custom field "specials" is available for all the new and existing posts in that post type.
I'll recommend these steps:
1. First, please update the "specials" field's settings, so that even the default/empty first option has some value, for example, '0'.
Example screenshot: hidden link
As a result, whenever you'll add a new post and no radio option is selected for the "specials" field, a custom field record with '0' will be added.
2. For the already added posts, you'll need to programmatically check for all the posts without any record for this custom field and set it.
Here is a code snippet for a shortcode that can work:
add_shortcode('update_empty_custom_fields', 'update_empty_custom_fields_func');
function update_empty_custom_fields_func($atts) {
$a = shortcode_atts( array(
'type' => '',
'field' => '',
'value' => ''
), $atts );
// get all published posts from a specific post type
$args = array(
'post_type' => $a['type'],
'posts_per_page' => -1,
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
// loop through those posts 1 by 1
foreach ($posts_array as $post) {
// get current value of the field
$current_value = get_post_meta( $post->ID, 'wpcf-'.$a['field'], true );
// if it is empty set the provided default value
if( empty($current_value) || !isset($current_value) ) {
update_post_meta( $post->ID, 'wpcf-'.$a['field'], $a['value'] );
}
}
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
To use it, you can place this shortcode in the content of any temporary page and view that page on the front-end:
Example:
[update_empty_custom_fields type="book" field="specials" value="0"]
Please replace "book" and "specials" with the actual slug of your custom post type and the custom field "specials", respectively.
Note: Once the custom field values have been updated, you can delete this temporary page and remove that custom code snippet from the website.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar