lundi 18 juillet 2016

Wordpress php if custom post type meta box checkbox is checked update db

I have a custom post type with the slug wedding_bookings. In that custom post type I have a checkbox.

On post save, if the checkbox is checked, I want to update post meta values in the db.

Here is how I have added the meta box checkbox:

add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
function your_prefix_meta_boxes( $meta_boxes ) {    
/* get users start   */
    $subscriberusers = get_users( 'orderby=nicename&role=subscriber' );
    if($subscriberusers){
        foreach ( $subscriberusers as $user ) {         
            $resultsUsers_sub[$user->ID] = $user->user_login;
        }   
    } else {
            $resultsUsers_sub[-1] = "No Data Found";            
    }
    $singerUsers = get_users( 'orderby=nicename&role=singer' );
    if($singerUsers){
        foreach ( $singerUsers as $suser ) {            
            $singerUsers_sub[$suser->ID] = $suser->user_login;
        }       
    } else {
        $singerUsers_sub[-1] = "No Data Found";
    }
    $packages_args = array(                                     
        'orderby'          => 'date',
        'order'            => 'DESC',                                       
        'post_type'        => 'package',
        'post_status'      => 'publish',
        'posts_per_page' => 999999999999                                        
    );
    $packages_posts_array = get_posts( $packages_args );        
    if($packages_posts_array){
        foreach($packages_posts_array as $packageloop){
            $packageMeta[$packageloop->ID] = $packageloop->post_title;
        }
    } else {
            $packageMeta[-1] = "No Data Found";
    }

$meta_boxes[] = array(
        'title'      => __( 'Payment', 'textdomain' ),
        'post_types' => 'wedding_booking',
        'fields'     => array(
            array(
                'id'   => 'paymentType',
                'name' => __( 'payment Type', 'textdomain' ),
                'type' => 'select',
                'options' => array(
                    $paymentMethod => __( $paymentMethod, 'textdomain' ),                    
                ),                                  
            ),
            array(
                'id'      => 'paymentAdvance',
                'name'    => __( 'Deposit Amount (%)', 'textdomain' ),
                'type'    => 'text',
                'std'   => $paymentAdvance,             
            ), 
            array(
                'id'      => 'depositAlreadyPaid',
                'name'    => __( 'Mark deposit as paid', 'textdomain' ),
                'type'    => 'checkbox',
            ),           
        ),
    );  

The checkbox has added to my custom post type and saves when I publish/save the post. What I want to be able to do is, on save, if the checkbox is checked execute the following function:

update_post_meta($post->ID, 'ispayment', '1');




Aucun commentaire:

Enregistrer un commentaire