On this page
wp_xmlrpc_server::set_custom_fields( int $post_id, array $fields )
Set custom fields for post.
Parameters
$post_idint Required-
Post ID.
$fieldsarray Required-
Custom fields.
Source
File: wp-includes/class-wp-xmlrpc-server.php. View all references
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'] );
}
}
}
Related
Uses
| Uses | Description |
|---|---|
| add_post_meta() wp-includes/post.php | Adds a meta field to the given post. |
| get_metadata_by_mid() wp-includes/meta.php | Retrieves metadata by meta ID. |
| update_metadata_by_mid() wp-includes/meta.php | Updates metadata by meta ID. |
| delete_metadata_by_mid() wp-includes/meta.php | Deletes metadata by meta ID. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| wp_unslash() wp-includes/formatting.php | Removes slashes from a string or recursively removes slashes from strings within an array. |
Used By
| Used By | Description |
|---|---|
| wp_xmlrpc_server::mw_editPost() wp-includes/class-wp-xmlrpc-server.php | Edit a post. |
| wp_xmlrpc_server::mw_newPost() wp-includes/class-wp-xmlrpc-server.php | Create a new post. |
| wp_xmlrpc_server::_insert_post() wp-includes/class-wp-xmlrpc-server.php | Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/set_custom_fields