Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 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's profile simonwheatley Owned by: ryan's profile 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 13 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
13 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
13 years ago

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

#3 @nacin
13 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
13 years ago

  • Keywords dev-feedback removed

@simonwheatley
13 years ago

Patch now adds WP_Error to wp_insert_post and wp_update_post

#5 @simonwheatley
13 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
13 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
13 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
13 years ago

Double-checked.

Note: See TracTickets for help on using tickets.