WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#23474 closed defect (bug) (fixed)

wp_update_post returns incorrect WP_Error on invalid ID (if $wp_error is set)

Reported by: simonwheatley Owned by: ryan
Milestone: 3.6 Priority: normal
Severity: normal Version: 3.5
Component: Warnings/Notices Keywords: has-patch
Focuses: Cc:

Description

$post_data = array(
	'ID' => 999, // Does not exist
	'post_title' => 'Test title',
);
$res = wp_update_post( $post_data, true );
error_log( $res->get_error_message() ); // 'Content, title, and excerpt are empty.'

The error is misleading, and further an array_merge notice is thrown as the null returned by get_post is not an array.

Attachments (1)

#23474.diff (958 bytes) - added by simonwheatley 5 years ago.
Patch now adds WP_Error to wp_insert_post and wp_update_post

Download all attachments as: .zip

Change History (10)

#1 @simonwheatley
5 years ago

Patch #23474.diff modifies wp_update_post to return 0 if the $wp_error param is false, and to return a WP_Error object with an appropriate message and code otherwise.

#2 @SergeyBiryukov
5 years ago

  • Component changed from General to Warnings/Notices
  • Milestone changed from Awaiting Review to 3.6

#3 @nacin
5 years ago

I guess wp_update_post() can hypothetically be used to create posts, but since wp_insert_post() then assumes if an ID is passed it is dealing with an update, things start to get wonky.

It would likely be best to fix this in both functions.

We can use "Invalid post ID." which is an existing string.

#4 @nacin
5 years ago

  • Keywords dev-feedback removed

@simonwheatley
5 years ago

Patch now adds WP_Error to wp_insert_post and wp_update_post

#5 @simonwheatley
5 years ago

Replying to nacin:

I guess wp_update_post() can hypothetically be used to create posts, but since wp_insert_post() then assumes if an ID is passed it is dealing with an update, things start to get wonky.

It would likely be best to fix this in both functions.

The patch now handles wp_update_post and wp_insert_post. Same error for both.

We can use "Invalid post ID." which is an existing string.

Done.

#7 @ryan
5 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In 23740:

Bail early with correct WP_Error when an invalid post ID is passed to wp_insert_post() and wp_update_post().

Props simonwheatley
fixes #23474

#8 @ryan
5 years ago

I had to move the invalid ID checks earlier in the functions to avoid things such as wp_slash() stomping the null response of get_post(). Please double check.

#9 @nacin
5 years ago

Double-checked.

Note: See TracTickets for help on using tickets.