• I’ve got a custom meta box created that has 4 radio box options. The idea is that you select one of the four, in turn it will select where on a specific page this post will show. Only 4 should be seen (top left, top right, bottom left, bottom right) so what I need to accomplish is to check all the postmeta in the database and delete any that have one of the four values so that only one each of those are there.

    I can update per id, check that there is any values and delete if there are.. for only that one id. I need to check all the id’s and not sure how to do that.

    Any help is greatly appreciated!

    function kp_art_save_featuredmain( $post_id, $post ) {
    
        if ( !isset( $_POST['kp_art_featuredmain_nonce'] ) || !wp_verify_nonce( $_POST['kp_art_featuredmain_nonce'], basename( __FILE__ ) ) )
            return $post_id;
    
        $post_type = get_post_type_object( $post->post_type );
        if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
            return $post_id;
    
        $new_meta_value = ( isset( $_POST['kp_art_featuredmain'] ) ? sanitize_html_class( $_POST['kp_art_featuredmain'] ) : '' );
        $meta_key = 'kp_art_featuredmain';
        $meta_value = get_post_meta( $post_id, $meta_key, true );
    
        $allposts = get_posts( 'numberposts=-1&post_type=post&post_status=any' );
    
    // this does not work... ??
    foreach( $allposts as $postinfo ) {
            delete_post_meta( $postinfo->ID, 'kp_art_featuredmain', $meta_value);
        }
    
        /* If a new meta value was added and there was no previous value, add it. */
        if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    
        /* If the new meta value does not match the old value, update it. */
        elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $post_id, $meta_key, $new_meta_value );
    
        /* If there is no new meta value but an old value exists, delete it. */
        elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $post_id, $meta_key, $meta_value );
    
    }
  • The topic ‘Meta box deleting’ is closed to new replies.