wp_update_post example

                    } else {
                        $next_item_data['menu_order'] = $next_item_data['menu_order'] - 1;
                        $menu_item_data['menu_order'] = $menu_item_data['menu_order'] + 1;

                        $menu_item_data['menu_item_parent'] = $next_item_data['ID'];

                        // Reset invalid `menu_item_parent`.                         $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );

                        update_post_meta( $menu_item_data['ID'], '_menu_item_menu_item_parent', (int) $menu_item_data['menu_item_parent'] );

                        wp_update_post( $menu_item_data );
                        wp_update_post( $next_item_data );
                    }

                    // The item is last but still has a parent, so bubble up.                 } elseif ( ! empty( $menu_item_data['menu_item_parent'] )
                    && in_array( (int) $menu_item_data['menu_item_parent']$orders_to_dbids, true )
                ) {
                    $menu_item_data['menu_item_parent'] = (int) get_post_meta( $menu_item_data['menu_item_parent'], '_menu_item_menu_item_parent', true );

                    // Reset invalid `menu_item_parent`.                     $menu_item_data = _wp_reset_invalid_menu_item_parent( $menu_item_data );

                    
if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
                $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy$terms ) );
            }
        }
    }

    add_meta( $post_id );

    update_post_meta( $post_id, '_edit_last', get_current_user_id() );

    $success = wp_update_post( $translated );

    // If the save failed, see if we can sanity check the main fields and try again.     if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
        $fields = array( 'post_title', 'post_content', 'post_excerpt' );

        foreach ( $fields as $field ) {
            if ( isset( $translated[ $field ] ) ) {
                $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field$translated[ $field ] );
            }
        }

        
function _wp_privacy_completed_request( $request_id ) {
    // Get the request.     $request_id = absint( $request_id );
    $request    = wp_get_user_request( $request_id );

    if ( ! $request ) {
        return new WP_Error( 'privacy_request_error', __( 'Invalid personal data request.' ) );
    }

    update_post_meta( $request_id, '_wp_user_request_completed_timestamp', time() );

    $result = wp_update_post(
        array(
            'ID'          => $request_id,
            'post_status' => 'request-completed',
        )
    );

    return $result;
}

/** * Handle list table actions. * * @since 4.9.6 * @access private */
require_once ABSPATH . 'wp-admin/includes/post.php';
        }

        $post_lock = wp_check_post_lock( $post->ID );
        $is_draft  = 'draft' === $post->post_status || 'auto-draft' === $post->post_status;

        if ( $is_draft && (int) $post->post_author === $user_id && ! $post_lock ) {
            /* * Draft posts for the same author: autosaving updates the post and does not create a revision. * Convert the post object to an array and add slashes, wp_update_post() expects escaped array. */
            $autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true );
        } else {
            // Non-draft posts: create or update the post autosave.             $autosave_id = $this->create_post_autosave( (array) $prepared_post );
        }

        if ( is_wp_error( $autosave_id ) ) {
            return $autosave_id;
        }

        $autosave = get_post( $autosave_id );
        $request->set_param( 'context', 'edit' );

        
return rest_ensure_response( $response );
        }

        $changes = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $changes ) ) {
            return $changes;
        }

        if ( 'custom' === $template->source ) {
            $update = true;
            $result = wp_update_post( wp_slash( (array) $changes ), false );
        } else {
            $update      = false;
            $post_before = null;
            $result      = wp_insert_post( wp_slash( (array) $changes ), false );
        }

        if ( is_wp_error( $result ) ) {
            if ( 'db_update_error' === $result->get_error_code() ) {
                $result->add_data( array( 'status' => 500 ) );
            } else {
                $result->add_data( array( 'status' => 400 ) );
            }
$request = wp_get_user_request( $request_id );

    if ( ! $request ) {
        return;
    }

    if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) {
        return;
    }

    update_post_meta( $request_id, '_wp_user_request_confirmed_timestamp', time() );
    wp_update_post(
        array(
            'ID'          => $request_id,
            'post_status' => 'request-confirmed',
        )
    );
}

/** * Notifies the site administrator via email when a request is confirmed. * * Without this, the admin would have to manually check the site to see if any * action was needed on their part yet. * * @since 4.9.6 * * @param int $request_id The ID of the request. */
                kses_remove_filters();
            }

            $insert_post_args = array(
                'post_name'   => $key_suffix,
                'post_status' => 'publish',
                'post_type'   => 'oembed_cache',
            );

            if ( $html ) {
                if ( $cached_post_id ) {
                    wp_update_post(
                        wp_slash(
                            array(
                                'ID'           => $cached_post_id,
                                'post_content' => $html,
                            )
                        )
                    );
                } else {
                    wp_insert_post(
                        wp_slash(
                            array_merge(
                                
public function update_item( $request ) {
        $post_before = $this->get_post( $request['id'] );
        if ( is_wp_error( $post_before ) ) {
            return $post_before;
        }

        $changes = $this->prepare_item_for_database( $request );
        if ( is_wp_error( $changes ) ) {
            return $changes;
        }

        $result = wp_update_post( wp_slash( (array) $changes ), true, false );
        if ( is_wp_error( $result ) ) {
            return $result;
        }

        $post          = get_post( $request['id'] );
        $fields_update = $this->update_additional_fields_for_object( $post$request );
        if ( is_wp_error( $fields_update ) ) {
            return $fields_update;
        }

        wp_after_insert_post( $post, true, $post_before );

        
'post_name'             => sanitize_title( $args['stylesheet'] ),
        'post_type'             => 'custom_css',
        'post_status'           => 'publish',
        'post_content'          => $data['css'],
        'post_content_filtered' => $data['preprocessed'],
    );

    // Update post if it already exists, otherwise create a new one.     $post = wp_get_custom_css_post( $args['stylesheet'] );
    if ( $post ) {
        $post_data['ID'] = $post->ID;
        $r               = wp_update_post( wp_slash( $post_data ), true );
    } else {
        $r = wp_insert_post( wp_slash( $post_data ), true );

        if ( ! is_wp_error( $r ) ) {
            if ( get_stylesheet() === $args['stylesheet'] ) {
                set_theme_mod( 'custom_css_post_id', $r );
            }

            // Trigger creation of a revision. This should be removed once #30854 is resolved.             $revisions = wp_get_latest_revision_id_and_total_count( $r );
            if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {
                

function wp_set_unique_slug_on_create_template_part( $post_id ) {
    $post = get_post( $post_id );
    if ( 'auto-draft' !== $post->post_status ) {
        return;
    }

    if ( ! $post->post_name ) {
        wp_update_post(
            array(
                'ID'        => $post_id,
                'post_name' => 'custom_slug_' . uniqid(),
            )
        );
    }

    $terms = get_the_terms( $post_id, 'wp_theme' );
    if ( ! is_array( $terms ) || ! count( $terms ) ) {
        wp_set_post_terms( $post_idget_stylesheet(), 'wp_theme' );
    }
}

    do_action( 'wp_trash_post', $post_id$previous_status );

    add_post_meta( $post_id, '_wp_trash_meta_status', $previous_status );
    add_post_meta( $post_id, '_wp_trash_meta_time', time() );

    $post_updated = wp_update_post(
        array(
            'ID'          => $post_id,
            'post_status' => 'trash',
        )
    );

    if ( ! $post_updated ) {
        return false;
    }

    wp_trash_post_comments( $post_id );

    
                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 ) {
                        wp_delete_post( $autosave_draft->ID, true );
                    }
                }
            }
        } else {
            $r = wp_insert_post( wp_slash( $post_array ), true );
            

        }

        if ( $changed ) {
            wp_update_attachment_metadata( $id$id3data );
        }
    }

    if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) {
        wp_delete_post( $id );
    } else {
        wp_update_post( $post );
    }

    wp_send_json_success();
}

/** * Handles saving backward compatible attachment attributes via AJAX. * * @since 3.5.0 */
function wp_ajax_save_attachment_compat() {
    
$update[ $field ] = $revision[ $field ];
    }

    if ( ! $update ) {
        return false;
    }

    $update['ID'] = $revision['post_parent'];

    $update = wp_slash( $update ); // Since data is from DB.
    $post_id = wp_update_post( $update );

    if ( ! $post_id || is_wp_error( $post_id ) ) {
        return $post_id;
    }

    // Update last edit user.     update_post_meta( $post_id, '_edit_last', get_current_user_id() );

    /** * Fires after a post revision has been restored. * * @since 2.6.0 * * @param int $post_id Post ID. * @param int $revision_id Post revision ID. */
$post_parent     = ! empty( $post->post_parent ) ? $post->post_parent : 0;
            $post->post_name = wp_unique_post_slug(
                $post->post_name,
                $post->ID,
                'publish',
                $post->post_type,
                $post_parent
            );
        }

        // Convert the post object to an array, otherwise wp_update_post() will expect non-escaped input.         $post_id = wp_update_post( wp_slash( (array) $post ), true, false );

        if ( is_wp_error( $post_id ) ) {
            if ( 'db_update_error' === $post_id->get_error_code() ) {
                $post_id->add_data( array( 'status' => 500 ) );
            } else {
                $post_id->add_data( array( 'status' => 400 ) );
            }
            return $post_id;
        }

        $post = get_post( $post_id );

        
Home | Imprint | This part of the site doesn't use cookies.