wp_xmlrpc_server::set_custom_fields()publicWP 2.5.0

Sets custom fields for post.

Метод класса: wp_xmlrpc_server{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$wp_xmlrpc_server = new wp_xmlrpc_server();
$wp_xmlrpc_server->set_custom_fields( $post_id, $fields );
$post_id(int) (обязательный)
Post ID.
$fields(массив) (обязательный)
Custom fields.

Список изменений

С версии 2.5.0 Введена.

Код wp_xmlrpc_server::set_custom_fields() WP 6.6.2

public function set_custom_fields( $post_id, $fields ) {
	$post_id = (int) $post_id;

	foreach ( (array) $fields as $meta ) {
		if ( isset( $meta['id'] ) ) {
			$meta['id'] = (int) $meta['id'];
			$pmeta      = get_metadata_by_mid( 'post', $meta['id'] );

			if ( ! $pmeta || $pmeta->post_id != $post_id ) {
				continue;
			}

			if ( isset( $meta['key'] ) ) {
				$meta['key'] = wp_unslash( $meta['key'] );
				if ( $meta['key'] !== $pmeta->meta_key ) {
					continue;
				}
				$meta['value'] = wp_unslash( $meta['value'] );
				if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) ) {
					update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
				}
			} elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
				delete_metadata_by_mid( 'post', $meta['id'] );
			}
		} elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
			add_post_meta( $post_id, $meta['key'], $meta['value'] );
		}
	}
}