Automattic\WooCommerce\Internal\ProductDownloads

DownloadableFileAttachments::handle_posts_results │ public │ WC 1.0

Drop a downloadable product file from the main query when it is requested as a single post.

With nothing found, WordPress shows a 404 instead of the attachment page or its redirect to the file, and feeds, embeds, and theme templates have no attachment to render.

Method of the class: DownloadableFileAttachments{}

No Hooks.

Returns

WP_Post[]|mixed.

Usage

$DownloadableFileAttachments = new DownloadableFileAttachments();
$DownloadableFileAttachments->handle_posts_results( $posts, $query );
$posts(WP_Post[]|mixed) (required)
Posts found by the query.
$query(WP_Query|mixed) (required)
The query.

DownloadableFileAttachments::handle_posts_results() code WC 11.3.0

public function handle_posts_results( $posts, $query ) {
	if ( ! is_array( $posts ) || 1 !== count( $posts ) || ! $query instanceof WP_Query || ! $query->is_main_query() || ! $query->is_singular() ) {
		return $posts;
	}

	$post = reset( $posts );

	if ( ! $post instanceof WP_Post || 'attachment' !== $post->post_type || ! $this->is_hidden_attachment( $post->ID ) ) {
		return $posts;
	}

	// Pretty permalinks set the queried object before posts are fetched, and feed titles print its title.
	unset( $query->queried_object, $query->queried_object_id );

	return array();
}