I'm working on a custom post type with a custom meta field post type (like post formats). I'm able to save and retrieve the post according to the meta key values. But the problem I'm facing is how to limit adding new post if that option of the field is already selected. ie, if that option is already selected user will get that option as disabled for ADD NEW POST.
Here what I've done so far:
function ps_add_meta_box() {
add_meta_box(
'ps_sectionid', 'Distinctive Person Format', 'ps_meta_box_callback', 'distinctiveperson','side','high'
); //you can change the 4th paramter i.e. post to custom post type name, if you want it for something else
}
add_action( 'add_meta_boxes', 'ps_add_meta_box' );
function ps_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'ps_meta_box', 'ps_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, 'ps_sectionid', true ); //my_key is a meta_key. Change it to whatever you want
?>
<!--<label for="ps_new_field"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br /> -->
<input type="radio" name="the_name_of_the_radio_buttons" value="value1" <?php checked( $value, 'value1' ); ?> >TYpe1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2" <?php checked( $value, 'value2' ); ?> >Type2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3" <?php checked( $value, 'value3' ); ?> >Type3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4" <?php checked( $value, 'value4' ); ?> >type4<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value5" <?php checked( $value, 'value5' ); ?> >Type5<br>
<?php
}
function ps_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( !isset( $_POST['ps_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $_POST['ps_meta_box_nonce'], 'ps_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize user input.
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
// Update the meta field in the database.
update_post_meta( $post_id, 'ps_sectionid', $new_meta_value );
}
add_action( 'save_post', 'ps_save_meta_box_data' );