WP_REST_Posts_Controller::handle_featured_media( int $featured_media, int $post_id ): bool|WP_Error

In this article

Determines the featured media based on a request param.

Parameters

$featured_mediaintrequired
Featured Media ID.
$post_idintrequired
Post ID.

Return

bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error.

Source

protected function handle_featured_media( $featured_media, $post_id ) {

	$featured_media = (int) $featured_media;
	if ( $featured_media ) {
		$result = set_post_thumbnail( $post_id, $featured_media );
		if ( $result ) {
			return true;
		} else {
			return new WP_Error(
				'rest_invalid_featured_media',
				__( 'Invalid featured media ID.' ),
				array( 'status' => 400 )
			);
		}
	} else {
		return delete_post_thumbnail( $post_id );
	}
}

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.