我正在使用自定义元字段处理自定义帖子类型post type (如post格式)。我能够根据元键值保存和检索帖子。但我面临的问题是,如果已经选择了该字段的选项,如何限制添加新帖子。即,如果已选择该选项,则用户将为禁用该选项ADD NEW POST.
以下是我迄今为止所做的:
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\' );