Inventory_Presser_Photo_Numberer::renumber_photos( int $post_id )

Reassigns sequence numbers to all photos attached to a vehicle post.

Description Description

Useful after multiple attachments or when a photo is deleted.


Top ↑

Parameters Parameters

$post_id

(int) (Required) The post ID of a vehicle.


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-photo-numberer.php

	public static function renumber_photos( $post_id ) {
		// Get all of this vehicle's photos that have numbers.
		$posts = get_children(
			array(
				'meta_key'    => apply_filters( 'invp_prefix_meta_key', 'photo_number' ),
				'orderby'     => 'meta_value_num',
				'post_parent' => $post_id,
				'post_type'   => 'attachment',
			)
		);

		// Get all of this post's attachments with or without numbers.
		$unnumbered_posts = get_children(
			array(
				'post_parent' => $post_id,
				'post_type'   => 'attachment',
			)
		);

		if ( count( $posts ) === count( $unnumbered_posts ) ) {
			return;
		}

		foreach ( $unnumbered_posts as $unnumbered_post ) {
			$have_post_ids = array_column( $posts, 'ID' );
			if ( ! in_array( $unnumbered_post->ID, $have_post_ids, true ) ) {
				$posts[ $unnumbered_post->ID ] = $unnumbered_post;
			}
		}

		$photo_count = count( $posts );
		if ( 0 === $photo_count ) {
			return;
		}

		$n = 1;
		foreach ( $posts as $child_post_id => $post ) {
			self::save_meta_photo_number( $child_post_id, $post_id, $n );
			if ( 1 === $n ) {
				// This is photo number 1, it should be the featured image.
				set_post_thumbnail( $post_id, $child_post_id );
			}
			++$n;
		}
		self::delete_photo_transients( $post_id );
	}