WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#38852 closed defect (bug) (fixed)

REST API Unable to Set Parent to 0

Reported by: lucasstark Owned by: rachelbaker
Milestone: 4.7 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Inside of WP_Rest_Posts_Controller::prepare_item_for_database there is a problem around the parent property.

The code checks if the parent is not empty, which, if you are trying to set the parent to 0 it will appear to be empty so the property is never setup for the database update.

// Parent.
		if ( ! empty( $schema['properties']['parent'] ) && ! empty( $request['parent'] ) ) {
			$parent = get_post( (int) $request['parent'] );

			if ( empty( $parent ) ) {
				return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent ID.' ), array( 'status' => 400 ) );
			}

			$prepared_post->post_parent = (int) $parent->ID;
		}

Attachments (1)

38852.1.diff (2.9 KB) - added by danielbachhuber 4 years ago.

Download all attachments as: .zip

Change History (5)

#1 @danielbachhuber
4 years ago

  • Keywords has-patch has-unit-tests added
  • Milestone changed from Awaiting Review to 4.7

#2 @danielbachhuber
4 years ago

  • Owner set to joehoyle
  • Status changed from new to assigned

#3 @rachelbaker
4 years ago

  • Owner changed from joehoyle to rachelbaker
  • Status changed from assigned to reviewing

#4 @rachelbaker
4 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 39289:

REST API: Allow parent property to be explicitly set to 0 when creating or updating a Post.

Props lucasstark, danielbachhuber.
Fixes #38852.

Note: See TracTickets for help on using tickets.