wp_create_post_autosave example

if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author
        && ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status )
    ) {
        $saved_post_id = edit_post();
    } else {
        $is_autosave = true;

        if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) {
            $_POST['post_status'] = 'draft';
        }

        $saved_post_id = wp_create_post_autosave( $post->ID );
    }

    if ( is_wp_error( $saved_post_id ) ) {
        wp_die( $saved_post_id->get_error_message() );
    }

    $query_args = array();

    if ( $is_autosave && $saved_post_id ) {
        $query_args['preview_id']    = $post->ID;
        $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );

        

        add_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5, 3 );
        if ( $changeset_post_id ) {
            if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
                // See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.                 add_filter( 'map_meta_cap', array( $this, 'grant_edit_post_capability_for_changeset' ), 10, 4 );

                $post_array['post_ID']   = $post_array['ID'];
                $post_array['post_type'] = 'customize_changeset';

                $r = wp_create_post_autosave( wp_slash( $post_array ) );

                remove_filter( 'map_meta_cap', array( $this, 'grant_edit_post_capability_for_changeset' ), 10 );
            } else {
                $post_array['edit_date'] = true; // Prevent date clearing.
                $r = wp_update_post( wp_slash( $post_array ), true );

                // Delete autosave revision for user when the changeset is updated.                 if ( ! empty( $args['user_id'] ) ) {
                    $autosave_draft = wp_get_post_autosave( $changeset_post_id$args['user_id'] );
                    if ( $autosave_draft ) {
                        
Home | Imprint | This part of the site doesn't use cookies.