Allow `wp_insert_post()` to accept a `meta_input` argument. Devs should use `register_meta()` to ensure that specific values specified by key are sanitized properly.

Adds unit test.

Props CoenJacobs, swissspidy.
Fixes #20451.


git-svn-id: https://develop.svn.wordpress.org/trunk@33910 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-09-05 19:49:53 +00:00
parent 7b3b1c12d5
commit 26dd8b5b08
2 changed files with 26 additions and 0 deletions

View File

@ -2775,6 +2775,7 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
*
* @since 1.0.0
* @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
* @since 4.4.0 A 'meta_input' array can now be passed to $postarr to add post meta data.
*
* @see sanitize_post()
* @global wpdb $wpdb WordPress database abstraction object.
@ -3172,6 +3173,12 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}
if ( ! empty( $postarr['meta_input'] ) ) {
foreach ( $postarr['meta_input'] as $field => $value ) {
update_post_meta( $post_ID, $field, $value );
}
}
$current_guid = get_post_field( 'guid', $post_ID );
// Set GUID.

View File

@ -452,6 +452,25 @@ class Tests_Post extends WP_UnitTestCase {
$this->assertContains( 'wptests_pt=' . $p, $post->guid );
}
/**
* @ticket 20451
*/
public function test_wp_insert_post_with_meta_input() {
$post_id = wp_insert_post( array(
'post_title' => '',
'post_content' => 'test',
'post_status' => 'publish',
'post_type' => 'post',
'meta_input' => array(
'hello' => 'world',
'foo' => 'bar'
)
) );
$this->assertEquals( 'world', get_post_meta( $post_id, 'hello', true ) );
$this->assertEquals( 'bar', get_post_meta( $post_id, 'foo', true ) );
}
/**
* @ticket 5364
*/