wp_insert_post example

$tag_names = array();

        if ( $tags && ! is_wp_error( $tags ) ) {
            $tag_names = wp_list_pluck( $tags, 'name' );
        }

        if ( $postarr['tags_input'] === $tag_names ) {
            unset( $postarr['tags_input'] );
        }
    }

    return wp_insert_post( $postarr$wp_error$fire_after_hooks );
}

/** * Publishes a post by transitioning the post status. * * @since 2.1.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int|WP_Post $post Post ID or post object. */
return;
    }

    // If we have a classic menu then convert it to blocks.     $classic_nav_menu_blocks = block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu );

    if ( empty( $classic_nav_menu_blocks ) ) {
        return;
    }

    // Create a new navigation menu from the classic menu.     $wp_insert_post_result = wp_insert_post(
        array(
            'post_content' => $classic_nav_menu_blocks,
            'post_title'   => $classic_nav_menu->name,
            'post_name'    => $classic_nav_menu->slug,
            'post_status'  => 'publish',
            'post_type'    => 'wp_navigation',
        ),
        true // So that we can check whether the result is an error.     );

    if ( is_wp_error( $wp_insert_post_result ) ) {
        

        $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data$content_struct );

        // Remove all null values to allow for using the insert/update post default values for those keys instead.         $post_data = array_filter(
            $post_data,
            static function D $value ) {
                return null !== $value;
            }
        );

        $post_id = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
        if ( is_wp_error( $post_id ) ) {
            return new IXR_Error( 500, $post_id->get_error_message() );
        }

        if ( ! $post_id ) {
            if ( $update ) {
                return new IXR_Error( 401, __( 'Sorry, the post could not be updated.' ) );
            } else {
                return new IXR_Error( 401, __( 'Sorry, the post could not be created.' ) );
            }
        }

        
        $classic_nav_menu_blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );

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

        if ( empty( $classic_nav_menu_blocks ) ) {
            return new WP_Error( 'cannot_convert_classic_menu', __( 'Unable to convert Classic Menu to blocks.' ) );
        }

        // Create a new navigation menu from the classic menu.         $classic_menu_fallback = wp_insert_post(
            array(
                'post_content' => $classic_nav_menu_blocks,
                'post_title'   => $classic_nav_menu->name,
                'post_name'    => $classic_nav_menu->slug,
                'post_status'  => 'publish',
                'post_type'    => 'wp_navigation',
            ),
            true // So that we can check whether the result is an error.         );

        return $classic_menu_fallback;
    }
$post_content = '';
    if ( ! empty( $_REQUEST['content'] ) ) {
        $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
    }

    $post_excerpt = '';
    if ( ! empty( $_REQUEST['excerpt'] ) ) {
        $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
    }

    if ( $create_in_db ) {
        $post_id = wp_insert_post(
            array(
                'post_title'  => __( 'Auto Draft' ),
                'post_type'   => $post_type,
                'post_status' => 'auto-draft',
            ),
            false,
            false
        );
        $post    = get_post( $post_id );
        if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) {
            set_post_format( $postget_option( 'default_post_format' ) );
        }
$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 ( ! is_wp_error( $r ) ) {
                $this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.             }
        }
        remove_filter( 'wp_insert_post_data', array( $this, 'preserve_insert_changeset_post_content' ), 5 );

        $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
        remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) );

        $response = array(
            
'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'] ) {
                wp_save_post_revision( $r );
            }
        }
$post_date = wp_resolve_post_date( $args['menu-item-post-date']$args['menu-item-post-date-gmt'] );
    if ( $post_date ) {
        $post['post_date'] = $post_date;
    }

    $update = 0 != $menu_item_db_id;

    // New menu item. Default is draft status.     if ( ! $update ) {
        $post['ID']          = 0;
        $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
        $menu_item_db_id     = wp_insert_post( $post, true, $fire_after_hooks );
        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
            return $menu_item_db_id;
        }

        /** * Fires immediately after a new navigation menu item has been added. * * @since 4.4.0 * * @see wp_update_nav_menu_item() * * @param int $menu_id ID of the updated menu. * @param int $menu_item_db_id ID of the new menu item. * @param array $args An array of arguments used to update/add the menu item. */
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(
                                $insert_post_args,
                                array(
                                    'post_content' => $html,
                                )
                            )
                        )
                    );
                }
            } elseif ( ! $cache ) {
                
if ( ! $post || empty( $post['ID'] ) ) {
        return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
    }

    if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) {
        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    }

    $post = _wp_post_revision_data( $post$autosave );
    $post = wp_slash( $post ); // Since data is from DB.
    $revision_id = wp_insert_post( $post, true );
    if ( is_wp_error( $revision_id ) ) {
        return $revision_id;
    }

    if ( $revision_id ) {
        /** * Fires once a revision has been saved. * * @since 2.6.0 * * @param int $revision_id Post revision ID. */

            $prepared_post->post_name = wp_unique_post_slug(
                $prepared_post->post_name,
                $prepared_post->id,
                'publish',
                $prepared_post->post_type,
                $prepared_post->post_parent
            );
        }

        $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true, false );

        if ( is_wp_error( $post_id ) ) {

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

            return $post_id;
        }

        
        if ( empty( $postarr['post_name'] ) ) {
            $postarr['post_name'] = sanitize_title( $postarr['post_title'] );
        }
        if ( ! isset( $postarr['meta_input'] ) ) {
            $postarr['meta_input'] = array();
        }
        $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
        $postarr['meta_input']['_customize_changeset_uuid']  = $this->manager->changeset_uuid();
        unset( $postarr['post_name'] );

        add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
        $r = wp_insert_post( wp_slash( $postarr ), true );
        remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );

        if ( is_wp_error( $r ) ) {
            return $r;
        } else {
            return get_post( $r );
        }
    }

    /** * Ajax handler for adding a new auto-draft post. * * @since 4.7.0 */
$post_title = xmlrpc_getposttitle( $content );

    if ( '' === trim( $post_title ) ) {
        $post_title = $subject;
    }

    $post_category = array( get_option( 'default_email_category' ) );

    $post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
    $post_data = wp_slash( $post_data );

    $post_ID = wp_insert_post( $post_data );
    if ( is_wp_error( $post_ID ) ) {
        echo "\n" . $post_ID->get_error_message();
    }

    // The post wasn't inserted or updated, for whatever reason. Better move forward to the next email.     if ( empty( $post_ID ) ) {
        continue;
    }

    /** * Fires after a post submitted by email is published. * * @since 1.2.0 * * @param int $post_ID The post ID. */
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 ) );
            }
            return $result;
        }

        
'request-pending',
                'request-confirmed',
            ),
            'fields'        => 'ids',
        )
    );

    if ( $requests_query->found_posts ) {
        return new WP_Error( 'duplicate_request', __( 'An incomplete personal data request for this email address already exists.' ) );
    }

    $request_id = wp_insert_post(
        array(
            'post_author'   => $user_id,
            'post_name'     => $action_name,
            'post_title'    => $email_address,
            'post_content'  => wp_json_encode( $request_data ),
            'post_status'   => 'request-' . $status,
            'post_type'     => 'user_request',
            'post_date'     => current_time( 'mysql', false ),
            'post_date_gmt' => current_time( 'mysql', true ),
        ),
        true
    );
Home | Imprint | This part of the site doesn't use cookies.