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:

101 to 200 of 684
    $post_category = array_filter( $postarr['post_category'] );
  }

  // Make sure we set a valid category.
  if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) {
    // 'post' requires at least one category.
    if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
      $post_category = array( get_option( 'default_category' ) );
    } else {
      $post_category = array();
    }
  }

  /*
	 * Don't allow contributors to set the post slug for pending review posts.
	 *
	 * For new posts check the primitive capability, for updates check the meta capability.
	 */
  $post_type_object = get_post_type_object( $post_type );

  if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
    $post_name = '';
  } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) {
    $post_name = '';
  }

  /*
	 * Create a valid post name. Drafts and pending posts are allowed to have
	 * an empty post name.
	 */
  if ( empty( $post_name ) ) {
    if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) {
      $post_name = sanitize_title( $post_title );
    } else {
      $post_name = '';
    }
  } else {
    // On updates, we need to check to see if it's using the old, fixed sanitization context.
    $check_name = sanitize_title( $post_name, '', 'old-save' );

    if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
      $post_name = $check_name;
    } else { // new post, or slug has changed.
      $post_name = sanitize_title( $post_name );
    }
  }

  /*
	 * Resolve the post date from any provided post date or post date GMT strings;
	 * if none are provided, the date will be set to now.
	 */
  $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
  if ( ! $post_date ) {
    if ( $wp_error ) {
      return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
    } else {
      return 0;
    }
  }

  if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
    if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) {
      $post_date_gmt = get_gmt_from_date( $post_date );
    } else {
      $post_date_gmt = '0000-00-00 00:00:00';
    }
  } else {
    $post_date_gmt = $postarr['post_date_gmt'];
  }

  if ( $update || '0000-00-00 00:00:00' === $post_date ) {
    $post_modified     = current_time( 'mysql' );
    $post_modified_gmt = current_time( 'mysql', 1 );
  } else {
    $post_modified     = $post_date;
    $post_modified_gmt = $post_date_gmt;
  }

  if ( 'attachment' !== $post_type ) {
    $now = gmdate( 'Y-m-d H:i:s' );

    if ( 'publish' === $post_status ) {
      if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) {
        $post_status = 'future';
      }
    } elseif ( 'future' === $post_status ) {

 View on GitHub View on Trac