Inventory_Presser_REST::exclude_deleted_posts( array $args, WP_REST_Request $request )

Strips post IDs that no longer exist from a REST API collection query so that WordPress core never attempts to prepare a response for a deleted vehicle. The previous approach of using rest_prepare_{post_type} was too late — WP core had already read properties off the null post object and generated PHP warnings before the filter ran.

On This Page


Parameters Parameters

$args

(array) (Required) WP_Query args built by the REST controller.

$request

(WP_REST_Request) (Required) The REST API request.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/class-rest.php

	public function exclude_deleted_posts( $args, $request ) {
		if ( empty( $args['post__in'] ) ) {
			return $args;
		}

		$existing = get_posts(
			array(
				'post__in'       => $args['post__in'],
				'post_type'      => INVP::POST_TYPE,
				'post_status'    => 'publish',
				'posts_per_page' => -1,
				'fields'         => 'ids',
			)
		);

		// When every requested ID has been deleted, pass 0 so WP doesn't fall
		// back to returning all posts.
		$args['post__in'] = ! empty( $existing ) ? $existing : array( 0 );

		return $args;
	}