Powered By Blogger

Wednesday, November 16, 2016

Programmatically change post visibility in wordpress

http://wordpress.stackexchange.com/questions/176437/programmatically-change-post-visibility-on-save-post-action-return-a-500



add_action( 'save_post', array( $this, 'save_meta_box' ), 13, 2 );

public function save_meta_box( $post_id, $post ) {
    if ( isset( $_POST['options'] ) ) {
        myplugin_set_options( $post_id, $_POST['options'] );

        remove_action( 'save_post',  array( $this, 'save_meta_box' ), 13, 2 );

        // switch post visibility
        switch ( $_POST['options']['type'] ) {
            case 'normal':
                wp_update_post( array( 'ID' => $post_id, 'post_status' => 'private' ));
                break;
            case 'extended':
                wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ));
                break;
        }

        add_action( 'save_post', array( $this, 'save_meta_box' ), 13, 2 );
    }
}

No comments:

Post a Comment