wp_xmlrpc_server::wp_getMediaLibrary() WordPress Method

The wp_xmlrpc_server::wp_getMediaLibrary() method allows you to retrieve a list of all the media items in your WordPress site's media library. This is a useful method if you want to programmatically manage your media library or if you want to display a list of all your media items on your website.

wp_xmlrpc_server::wp_getMediaLibrary( array $args ) #

Retrieves a collection of media library items (or attachments)


Description

Besides the common blog_id (unused), username, and password arguments, it takes a filter array as last argument.

Accepted ‘filter’ keys are ‘parent_id’, ‘mime_type’, ‘offset’, and ‘number’.

The defaults are as follows:

  • ‘number’ – Default is 5. Total number of media items to retrieve.
  • ‘offset’ – Default is 0. See WP_Query::query() for more.
  • ‘parent_id’ – Default is ”. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  • ‘mime_type’ – Default is ”. Filter by mime type (e.g., ‘image/jpeg’, ‘application/pdf’)

Top ↑

Parameters

$args

(array)(Required)Method arguments. Note: arguments must be ordered as documented.

  • 'blog_id'
    (int) (unused)
  • 'username'
    (string)
  • 'password'
    (string)
  • 'struct'
    (array)


Top ↑

Return

(array|IXR_Error) Contains a collection of media items. See wp_xmlrpc_server::wp_getMediaItem() for a description of each item contents


Top ↑

Source

File: wp-includes/class-wp-xmlrpc-server.php

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

		$username = $args[1];
		$password = $args[2];
		$struct   = isset( $args[3] ) ? $args[3] : array();

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

		if ( ! current_user_can( 'upload_files' ) ) {
			return new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) );
		}

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

		$parent_id = ( isset( $struct['parent_id'] ) ) ? absint( $struct['parent_id'] ) : '';
		$mime_type = ( isset( $struct['mime_type'] ) ) ? $struct['mime_type'] : '';
		$offset    = ( isset( $struct['offset'] ) ) ? absint( $struct['offset'] ) : 0;
		$number    = ( isset( $struct['number'] ) ) ? absint( $struct['number'] ) : -1;

		$attachments = get_posts(
			array(
				'post_type'      => 'attachment',
				'post_parent'    => $parent_id,
				'offset'         => $offset,
				'numberposts'    => $number,
				'post_mime_type' => $mime_type,
			)
		);

		$attachments_struct = array();

		foreach ( $attachments as $attachment ) {
			$attachments_struct[] = $this->_prepare_media_item( $attachment );
		}

		return $attachments_struct;
	}


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More