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() {

                '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>



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'] ) ) {

        // Verify that the nonce is valid.
        if ( !wp_verify_nonce( $_POST['ps_meta_box_nonce'], 'ps_meta_box' ) ) {

        // 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 ) {

        // Check the user's permissions.
        if ( !current_user_can( 'edit_post', $post_id ) ) {

        // 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' );

1 Answer 1


If I understood you correctly, you want to prevent users from selecting a radio option, which have been already selected on another post, when the user is adding a new post, right?

I think you need to first check if the user is on the "add new" page in your metabox. You can do that with the global $pagenow variable. That will give you post-new.php when you're on the "add new" page.

(Side note: You could also wrap the check in a custom function like seen here, How to determine whether we are in add New page/post/CPT or in edit page/post/CPT in wordpress admin?)

Then do a conditional check to make sure it is the "add new" page. If so get all the values that have already been selected on other posts. This is somewhat unfamiliar territory for me, but I found a promising old thread on the matter - How to get a meta value from all post

I think the custom get_meta_values function shared on the old thread should do the trick for you. It might need some tweaking, but test it and see how it works.

And third, when you have the array of previously selected values, you can use it to mark matching options as selected / disabled.

Hopefully I didn't miss the mark too badly and answered you're question. I'm sorry I don't have any code of my own to share with you. This is the idea and concept that popped into my mind after reading your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.