wp_insert_post() – Insert or update a post.

You appear to be a bot. Output may be restricted

Description

Insert or update a post.

If the $postarr parameter has 'ID' set to a value, then post will be updated. You can set the post date manually, by setting the values for 'post_date' and 'post_date_gmt' keys. You can close the comments or open the comments by setting the value for 'comment_status' key.

Usage

$int|WP_Error = wp_insert_post( $postarr, $wp_error, $fire_after_hooks );

Parameters

$postarr
( array ) required – { An array of elements that make up a post to update or insert.
$ID
( int ) required – The post ID. If equal to something other than 0, the post with that ID will be updated. Default 0.
$post_author
( int ) required – The ID of the user who added the post. Default is the current user ID.
$post_date
( string ) required – The date of the post. Default is the current time.
$post_date_gmt
( string ) required – The date of the post in the GMT timezone. Default is the value of `$post_date`.
$post_content
( string ) required – The post content. Default empty.
$post_content_filtered
( string ) required – The filtered post content. Default empty.
$post_title
( string ) required – The post title. Default empty.
$post_excerpt
( string ) required – The post excerpt. Default empty.
$post_status
( string ) required – The post status. Default 'draft'.
$post_type
( string ) required – The post type. Default 'post'.
$comment_status
( string ) required – Whether the post can accept comments. Accepts 'open' or 'closed'. Default is the value of 'default_comment_status' option.
$ping_status
( string ) required – Whether the post can accept pings. Accepts 'open' or 'closed'. Default is the value of 'default_ping_status' option.
$post_password
( string ) required – The password to access the post. Default empty.
$post_name
( string ) required – The post name. Default is the sanitized post title when creating a new post.
$to_ping
( string ) required – Space or carriage return-separated list of URLs to ping. Default empty.
$pinged
( string ) required – Space or carriage return-separated list of URLs that have been pinged. Default empty.
$post_modified
( string ) required – The date when the post was last modified. Default is the current time.
$post_modified_gmt
( string ) required – The date when the post was last modified in the GMT timezone. Default is the current time.
$post_parent
( int ) required – Set this for the post it belongs to, if any. Default 0.
$menu_order
( int ) required – The order the post should be displayed in. Default 0.
$post_mime_type
( string ) required – The mime type of the post. Default empty.
$guid
( string ) required – Global Unique ID for referencing the post. Default empty.
$import_id
( int ) required – The post ID to be used when inserting a new post. If specified, must not match any existing post ID. Default 0.
$post_category
( int[] ) required – Array of category IDs. Defaults to value of the 'default_category' option.
$tags_input
( array ) required – Array of tag names, slugs, or IDs. Default empty.
$tax_input
( array ) required – An array of taxonomy terms keyed by their taxonomy name. If the taxonomy is hierarchical, the term list needs to be either an array of term IDs or a comma-separated string of IDs. If the taxonomy is non-hierarchical, the term list can be an array that contains term names or slugs, or a comma-separated string of names or slugs. This is because, in hierarchical taxonomy, child terms can have the same names with different parent terms, so the only way to connect them is using ID. Default empty.
$meta_input
( array ) required – Array of post meta values keyed by their post meta key. Default empty. }
$wp_error
( bool ) optional – Optional. Whether to return a WP_Error on failure. Default false.
$fire_after_hooks
( bool ) optional default: 1 – Optional. Whether to fire the after insert hooks. Default true.

Returns

int|WP_Error The post ID on success. The value 0 or WP_Error on failure.

Source

File name: wordpress/wp-includes/post.php


Lines:

201 to 300 of 684
  if ( empty( $postarr['comment_status'] ) ) {
    if ( $update ) {
      $comment_status = 'closed';
    } else {
      $comment_status = get_default_comment_status( $post_type );
    }
  } else {
    $comment_status = $postarr['comment_status'];
  }

  // These variables are needed by compact() later.
  $post_content_filtered = $postarr['post_content_filtered'];
  $post_author           = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
  $ping_status           = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
  $to_ping               = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
  $pinged                = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
  $import_id             = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;

  /*
	 * The 'wp_insert_post_parent' filter expects all variables to be present.
	 * Previously, these variables would have already been extracted
	 */
  if ( isset( $postarr['menu_order'] ) ) {
    $menu_order = (int) $postarr['menu_order'];
  } else {
    $menu_order = 0;
  }

  $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
  if ( 'private' === $post_status ) {
    $post_password = '';
  }

  if ( isset( $postarr['post_parent'] ) ) {
    $post_parent = (int) $postarr['post_parent'];
  } else {
    $post_parent = 0;
  }

  $new_postarr = array_merge(
    array(
      'ID' => $post_ID,
    ),
    compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) )
  );

  
/**
 * Filters the post parent -- used to check for and prevent hierarchy loops.
 *
 * @since 3.1.0
 *
 * @param int   $post_parent Post parent ID.
 * @param int   $post_ID     Post ID.
 * @param array $new_postarr Array of parsed post data.
 * @param array $postarr     Array of sanitized, but otherwise unmodified post data.
 */
  $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr );

  /*
	 * If the post is being untrashed and it has a desired slug stored in post meta,
	 * reassign it.
	 */
  if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
    $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );

    if ( $desired_post_slug ) {
      delete_post_meta( $post_ID, '_wp_desired_post_slug' );
      $post_name = $desired_post_slug;
    }
  }

  // If a trashed post has the desired slug, change it and let this post have it.
  if ( 'trash' !== $post_status && $post_name ) {
    
/**
 * Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post.
 *
 * @since 5.4.0
 *
 * @param bool   $add_trashed_suffix Whether to attempt to add the suffix.
 * @param string $post_name          The name of the post being updated.
 * @param int    $post_ID            Post ID.
 */
    $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_ID );

    if ( $add_trashed_suffix ) {
      wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
    }
  }

  // When trashing an existing post, change its slug to allow non-trashed posts to use it.
  if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {

 View on GitHub View on Trac