wp_xmlrpc_server::mw_getPost( array $args ): array|IXR_Error

In this article

Retrieves a post.

Parameters

$argsarrayrequired
Method arguments. Note: arguments must be ordered as documented.
  • 0 int
    Post ID.
  • 1 string
    Username.
  • 2 string
    Password.

Return

array|IXR_Error

Source

public function mw_getPost( $args ) {
	$this->escape( $args );

	$post_id  = (int) $args[0];
	$username = $args[1];
	$password = $args[2];

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

	$postdata = get_post( $post_id, ARRAY_A );
	if ( ! $postdata ) {
		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.' ) );
	}

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

	if ( '' !== $postdata['post_date'] ) {
		$post_date         = $this->_convert_date( $postdata['post_date'] );
		$post_date_gmt     = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
		$post_modified     = $this->_convert_date( $postdata['post_modified'] );
		$post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );

		$categories = array();
		$catids     = wp_get_post_categories( $post_id );
		foreach ( $catids as $catid ) {
			$categories[] = get_cat_name( $catid );
		}

		$tagnames = array();
		$tags     = wp_get_post_tags( $post_id );
		if ( ! empty( $tags ) ) {
			foreach ( $tags as $tag ) {
				$tagnames[] = $tag->name;
			}
			$tagnames = implode( ', ', $tagnames );
		} else {
			$tagnames = '';
		}

		$post = get_extended( $postdata['post_content'] );
		$link = get_permalink( $postdata['ID'] );

		// Get the author info.
		$author = get_userdata( $postdata['post_author'] );

		$allow_comments = ( 'open' === $postdata['comment_status'] ) ? 1 : 0;
		$allow_pings    = ( 'open' === $postdata['ping_status'] ) ? 1 : 0;

		// Consider future posts as published.
		if ( 'future' === $postdata['post_status'] ) {
			$postdata['post_status'] = 'publish';
		}

		// Get post format.
		$post_format = get_post_format( $post_id );
		if ( empty( $post_format ) ) {
			$post_format = 'standard';
		}

		$sticky = false;
		if ( is_sticky( $post_id ) ) {
			$sticky = true;
		}

		$enclosure = array();
		foreach ( (array) get_post_custom( $post_id ) as $key => $val ) {
			if ( 'enclosure' === $key ) {
				foreach ( (array) $val as $enc ) {
					$encdata             = explode( "\n", $enc );
					$enclosure['url']    = trim( htmlspecialchars( $encdata[0] ) );
					$enclosure['length'] = (int) trim( $encdata[1] );
					$enclosure['type']   = trim( $encdata[2] );
					break 2;
				}
			}
		}

		$resp = array(
			'dateCreated'            => $post_date,
			'userid'                 => $postdata['post_author'],
			'postid'                 => $postdata['ID'],
			'description'            => $post['main'],
			'title'                  => $postdata['post_title'],
			'link'                   => $link,
			'permaLink'              => $link,
			// Commented out because no other tool seems to use this.
			// 'content' => $entry['post_content'],
			'categories'             => $categories,
			'mt_excerpt'             => $postdata['post_excerpt'],
			'mt_text_more'           => $post['extended'],
			'wp_more_text'           => $post['more_text'],
			'mt_allow_comments'      => $allow_comments,
			'mt_allow_pings'         => $allow_pings,
			'mt_keywords'            => $tagnames,
			'wp_slug'                => $postdata['post_name'],
			'wp_password'            => $postdata['post_password'],
			'wp_author_id'           => (string) $author->ID,
			'wp_author_display_name' => $author->display_name,
			'date_created_gmt'       => $post_date_gmt,
			'post_status'            => $postdata['post_status'],
			'custom_fields'          => $this->get_custom_fields( $post_id ),
			'wp_post_format'         => $post_format,
			'sticky'                 => $sticky,
			'date_modified'          => $post_modified,
			'date_modified_gmt'      => $post_modified_gmt,
		);

		if ( ! empty( $enclosure ) ) {
			$resp['enclosure'] = $enclosure;
		}

		$resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );

		return $resp;
	} else {
		return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
	}
}

Hooks

do_action( ‘xmlrpc_call’, string $name, array|string $args, wp_xmlrpc_server $server )

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

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.