Title: wp_xmlrpc_server::mw_editPost
Published: April 25, 2014
Last modified: April 28, 2025

---

# wp_xmlrpc_server::mw_editPost( array $args ): true|󠀁[IXR_Error](https://developer.wordpress.org/reference/classes/ixr_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#wp--skip-link--target)

Edits a post.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#parameters)󠁿

 `$args`arrayrequired

Method arguments. Note: arguments must be ordered as documented.

 * `0` int
 * Post ID.
 * `1` string
 * Username.
 * `2` string
 * Password.
 * `3` array
 * Content structure.
 * `4` int
 * Optional. Publish flag. 0 for draft, 1 for publish. Default 0.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#return)󠁿

 true|[IXR_Error](https://developer.wordpress.org/reference/classes/ixr_error/) 
True on success.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#source)󠁿

    ```php
    public function mw_editPost( $args ) {
    	$this->escape( $args );

    	$post_id        = (int) $args[0];
    	$username       = $args[1];
    	$password       = $args[2];
    	$content_struct = $args[3];
    	$publish        = isset( $args[4] ) ? $args[4] : 0;

    	$user = $this->login( $username, $password );
    	if ( ! $user ) {
    		return $this->error;
    	}

    	/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    	do_action( 'xmlrpc_call', 'metaWeblog.editPost', $args, $this );

    	$postdata = get_post( $post_id, ARRAY_A );

    	/*
    	 * If there is no post data for the give post ID, stop now and return an error.
    	 * Otherwise a new post will be created (which was the old behavior).
    	 */
    	if ( ! $postdata || empty( $postdata['ID'] ) ) {
    		return new IXR_Error( 404, __( 'Invalid post ID.' ) );
    	}

    	if ( ! current_user_can( 'edit_post', $post_id ) ) {
    		return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
    	}

    	// Use wp.editPost to edit post types other than post and page.
    	if ( ! in_array( $postdata['post_type'], array( 'post', 'page' ), true ) ) {
    		return new IXR_Error( 401, __( 'Invalid post type.' ) );
    	}

    	// Thwart attempt to change the post type.
    	if ( ! empty( $content_struct['post_type'] ) && ( $content_struct['post_type'] !== $postdata['post_type'] ) ) {
    		return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
    	}

    	// Check for a valid post format if one was given.
    	if ( isset( $content_struct['wp_post_format'] ) ) {
    		$content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
    		if ( ! array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
    			return new IXR_Error( 404, __( 'Invalid post format.' ) );
    		}
    	}

    	$this->escape( $postdata );

    	$post_id        = $postdata['ID'];
    	$post_content   = $postdata['post_content'];
    	$post_title     = $postdata['post_title'];
    	$post_excerpt   = $postdata['post_excerpt'];
    	$post_password  = $postdata['post_password'];
    	$post_parent    = $postdata['post_parent'];
    	$post_type      = $postdata['post_type'];
    	$menu_order     = $postdata['menu_order'];
    	$ping_status    = $postdata['ping_status'];
    	$comment_status = $postdata['comment_status'];

    	// Let WordPress manage slug if none was provided.
    	$post_name = $postdata['post_name'];
    	if ( isset( $content_struct['wp_slug'] ) ) {
    		$post_name = $content_struct['wp_slug'];
    	}

    	// Only use a password if one was given.
    	if ( isset( $content_struct['wp_password'] ) ) {
    		$post_password = $content_struct['wp_password'];
    	}

    	// Only set a post parent if one was given.
    	if ( isset( $content_struct['wp_page_parent_id'] ) ) {
    		$post_parent = $content_struct['wp_page_parent_id'];
    	}

    	// Only set the 'menu_order' if it was given.
    	if ( isset( $content_struct['wp_page_order'] ) ) {
    		$menu_order = $content_struct['wp_page_order'];
    	}

    	$page_template = '';
    	if ( ! empty( $content_struct['wp_page_template'] ) && 'page' === $post_type ) {
    		$page_template = $content_struct['wp_page_template'];
    	}

    	$post_author = $postdata['post_author'];

    	// If an author ID was provided then use it instead.
    	if ( isset( $content_struct['wp_author_id'] ) ) {
    		// Check permissions if attempting to switch author to or from another user.
    		if ( $user->ID !== (int) $content_struct['wp_author_id'] || $user->ID !== (int) $post_author ) {
    			switch ( $post_type ) {
    				case 'post':
    					if ( ! current_user_can( 'edit_others_posts' ) ) {
    						return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the post author as this user.' ) );
    					}
    					break;
    				case 'page':
    					if ( ! current_user_can( 'edit_others_pages' ) ) {
    						return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the page author as this user.' ) );
    					}
    					break;
    				default:
    					return new IXR_Error( 401, __( 'Invalid post type.' ) );
    			}
    			$post_author = $content_struct['wp_author_id'];
    		}
    	}

    	if ( isset( $content_struct['mt_allow_comments'] ) ) {
    		if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
    			switch ( $content_struct['mt_allow_comments'] ) {
    				case 'closed':
    					$comment_status = 'closed';
    					break;
    				case 'open':
    					$comment_status = 'open';
    					break;
    				default:
    					$comment_status = get_default_comment_status( $post_type );
    					break;
    			}
    		} else {
    			switch ( (int) $content_struct['mt_allow_comments'] ) {
    				case 0:
    				case 2:
    					$comment_status = 'closed';
    					break;
    				case 1:
    					$comment_status = 'open';
    					break;
    				default:
    					$comment_status = get_default_comment_status( $post_type );
    					break;
    			}
    		}
    	}

    	if ( isset( $content_struct['mt_allow_pings'] ) ) {
    		if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) {
    			switch ( $content_struct['mt_allow_pings'] ) {
    				case 'closed':
    					$ping_status = 'closed';
    					break;
    				case 'open':
    					$ping_status = 'open';
    					break;
    				default:
    					$ping_status = get_default_comment_status( $post_type, 'pingback' );
    					break;
    			}
    		} else {
    			switch ( (int) $content_struct['mt_allow_pings'] ) {
    				case 0:
    					$ping_status = 'closed';
    					break;
    				case 1:
    					$ping_status = 'open';
    					break;
    				default:
    					$ping_status = get_default_comment_status( $post_type, 'pingback' );
    					break;
    			}
    		}
    	}

    	if ( isset( $content_struct['title'] ) ) {
    		$post_title = $content_struct['title'];
    	}

    	if ( isset( $content_struct['description'] ) ) {
    		$post_content = $content_struct['description'];
    	}

    	$post_category = array();
    	if ( isset( $content_struct['categories'] ) ) {
    		$catnames = $content_struct['categories'];
    		if ( is_array( $catnames ) ) {
    			foreach ( $catnames as $cat ) {
    				$post_category[] = get_cat_ID( $cat );
    			}
    		}
    	}

    	if ( isset( $content_struct['mt_excerpt'] ) ) {
    		$post_excerpt = $content_struct['mt_excerpt'];
    	}

    	$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';

    	$post_status = $publish ? 'publish' : 'draft';
    	if ( isset( $content_struct[ "{$post_type}_status" ] ) ) {
    		switch ( $content_struct[ "{$post_type}_status" ] ) {
    			case 'draft':
    			case 'pending':
    			case 'private':
    			case 'publish':
    				$post_status = $content_struct[ "{$post_type}_status" ];
    				break;
    			default:
    				$post_status = $publish ? 'publish' : 'draft';
    				break;
    		}
    	}

    	$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();

    	if ( 'publish' === $post_status || 'private' === $post_status ) {
    		if ( 'page' === $post_type && ! current_user_can( 'publish_pages' ) ) {
    			return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this page.' ) );
    		} elseif ( ! current_user_can( 'publish_posts' ) ) {
    			return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
    		}
    	}

    	if ( $post_more ) {
    		$post_content = $post_content . '<!--more-->' . $post_more;
    	}

    	$to_ping = '';
    	if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
    		$to_ping = $content_struct['mt_tb_ping_urls'];
    		if ( is_array( $to_ping ) ) {
    			$to_ping = implode( ' ', $to_ping );
    		}
    	}

    	// Do some timestamp voodoo.
    	if ( ! empty( $content_struct['date_created_gmt'] ) ) {
    		// We know this is supposed to be GMT, so we're going to slap that Z on there by force.
    		$date_created = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
    	} elseif ( ! empty( $content_struct['dateCreated'] ) ) {
    		$date_created = $content_struct['dateCreated']->getIso();
    	}

    	// Default to not flagging the post date to be edited unless it's intentional.
    	$edit_date = false;

    	if ( ! empty( $date_created ) ) {
    		$post_date     = iso8601_to_datetime( $date_created );
    		$post_date_gmt = iso8601_to_datetime( $date_created, 'gmt' );

    		// Flag the post date to be edited.
    		$edit_date = true;
    	} else {
    		$post_date     = $postdata['post_date'];
    		$post_date_gmt = $postdata['post_date_gmt'];
    	}

    	$newpost = array(
    		'ID' => $post_id,
    	);

    	$newpost += compact(
    		'post_content',
    		'post_title',
    		'post_category',
    		'post_status',
    		'post_excerpt',
    		'comment_status',
    		'ping_status',
    		'edit_date',
    		'post_date',
    		'post_date_gmt',
    		'to_ping',
    		'post_name',
    		'post_password',
    		'post_parent',
    		'menu_order',
    		'post_author',
    		'tags_input',
    		'page_template'
    	);

    	// We've got all the data -- post it.
    	$result = wp_update_post( $newpost, true );
    	if ( is_wp_error( $result ) ) {
    		return new IXR_Error( 500, $result->get_error_message() );
    	}

    	if ( ! $result ) {
    		return new IXR_Error( 500, __( 'Sorry, the post could not be updated.' ) );
    	}

    	// Only posts can be sticky.
    	if ( 'post' === $post_type && isset( $content_struct['sticky'] ) ) {
    		$data              = $newpost;
    		$data['sticky']    = $content_struct['sticky'];
    		$data['post_type'] = 'post';
    		$error             = $this->_toggle_sticky( $data, true );
    		if ( $error ) {
    			return $error;
    		}
    	}

    	if ( isset( $content_struct['custom_fields'] ) ) {
    		$this->set_custom_fields( $post_id, $content_struct['custom_fields'] );
    	}

    	if ( isset( $content_struct['wp_post_thumbnail'] ) ) {

    		// Empty value deletes, non-empty value adds/updates.
    		if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
    			delete_post_thumbnail( $post_id );
    		} else {
    			if ( set_post_thumbnail( $post_id, $content_struct['wp_post_thumbnail'] ) === false ) {
    				return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
    			}
    		}
    		unset( $content_struct['wp_post_thumbnail'] );
    	}

    	// Handle enclosures.
    	$enclosure = isset( $content_struct['enclosure'] ) ? $content_struct['enclosure'] : null;
    	$this->add_enclosure_if_new( $post_id, $enclosure );

    	$this->attach_uploads( $post_id, $post_content );

    	// Handle post formats if assigned, validation is handled earlier in this function.
    	if ( isset( $content_struct['wp_post_format'] ) ) {
    		set_post_format( $post_id, $content_struct['wp_post_format'] );
    	}

    	/**
    	 * Fires after a post has been successfully updated via the XML-RPC MovableType API.
    	 *
    	 * @since 3.4.0
    	 *
    	 * @param int   $post_id ID of the updated post.
    	 * @param array $args    An array of arguments to update the post.
    	 */
    	do_action( 'xmlrpc_call_success_mw_editPost', $post_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-xmlrpc-server.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-xmlrpc-server.php#L5767)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-xmlrpc-server.php#L5767-L6104)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#hooks)󠁿

 [do_action( ‘xmlrpc_call’, string $name, array|string $args, wp_xmlrpc_server $server )](https://developer.wordpress.org/reference/hooks/xmlrpc_call/)

Fires after the XML-RPC user has been authenticated but before the rest of the method
logic begins.

 [do_action( ‘xmlrpc_call_success_mw_editPost’, int $post_id, array $args )](https://developer.wordpress.org/reference/hooks/xmlrpc_call_success_mw_editpost/)

Fires after a post has been successfully updated via the XML-RPC MovableType API.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_xmlrpc_server::_toggle_sticky()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/_toggle_sticky/)`wp-includes/class-wp-xmlrpc-server.php` |

Encapsulates the logic for sticking a post and determining if the user has permission to do so.

  | 
| [get_default_comment_status()](https://developer.wordpress.org/reference/functions/get_default_comment_status/)`wp-includes/comment.php` |

Gets the default comment status for a post type.

  | 
| [iso8601_to_datetime()](https://developer.wordpress.org/reference/functions/iso8601_to_datetime/)`wp-includes/formatting.php` |

Given an ISO 8601 (Ymd\TH:i:sO) date, returns a MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].

  | 
| [get_cat_ID()](https://developer.wordpress.org/reference/functions/get_cat_id/)`wp-includes/category.php` |

Retrieves the ID of a category from its name.

  | 
| [delete_post_thumbnail()](https://developer.wordpress.org/reference/functions/delete_post_thumbnail/)`wp-includes/post.php` |

Removes the thumbnail (featured image) from the given post.

  | 
| [set_post_thumbnail()](https://developer.wordpress.org/reference/functions/set_post_thumbnail/)`wp-includes/post.php` |

Sets the post thumbnail (featured image) for the given post.

  | 
| [wp_update_post()](https://developer.wordpress.org/reference/functions/wp_update_post/)`wp-includes/post.php` |

Updates a post with new post data.

  | 
| [get_post_format_strings()](https://developer.wordpress.org/reference/functions/get_post_format_strings/)`wp-includes/post-formats.php` |

Returns an array of post format slugs to their translated and pretty display versions

  | 
| [set_post_format()](https://developer.wordpress.org/reference/functions/set_post_format/)`wp-includes/post-formats.php` |

Assign a format to a post

  | 
| [wp_xmlrpc_server::add_enclosure_if_new()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/add_enclosure_if_new/)`wp-includes/class-wp-xmlrpc-server.php` |

Adds an enclosure to a post if it’s new.

  | 
| [wp_xmlrpc_server::attach_uploads()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/attach_uploads/)`wp-includes/class-wp-xmlrpc-server.php` |

Attaches an upload to a post.

  | 
| [wp_xmlrpc_server::set_custom_fields()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/set_custom_fields/)`wp-includes/class-wp-xmlrpc-server.php` |

Sets custom fields for post.

  | 
| [IXR_Error::__construct()](https://developer.wordpress.org/reference/classes/ixr_error/__construct/)`wp-includes/IXR/class-IXR-error.php` |

PHP5 constructor.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [sanitize_key()](https://developer.wordpress.org/reference/functions/sanitize_key/)`wp-includes/formatting.php` |

Sanitizes a string key.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  | 
| [wp_xmlrpc_server::escape()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/escape/)`wp-includes/class-wp-xmlrpc-server.php` |

Escapes string or array of strings for database.

  | 
| [wp_xmlrpc_server::login()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/login/)`wp-includes/class-wp-xmlrpc-server.php` |

Logs user in.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 16 more](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#)

| Used by | Description | 
| [wp_xmlrpc_server::wp_editPage()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/wp_editpage/)`wp-includes/class-wp-xmlrpc-server.php` |

Edits a page.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/mw_editpost/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_xmlrpc_server%2Fmw_editpost%2F)
before being able to contribute a note or feedback.