iaffpro_update_attributes_in_post_by_post_id()

Update image attributes of all images in a given post. Post can be specified by passing post ID or post object.

Image attributes will be updated as per the Bulk Updater Settings of Image Attributes Pro.

iaffpro_update_attributes_in_post_by_post_id( $post_id );
  • Requires Image Attributes Pro 3.0 and above.
  • Accepts the post ID ($post_id) as a parameter.
  • Does not return anything.
  • Can be used for posts, pages, WooCommerce products and any custom post type.

Example Usages

// Update image attributes of all images for a post with ID 247
iaffpro_update_attributes_in_post_by_post_id( 247 );
/**
 * Update image attributes as per the Bulk Updater Settings of Image Attributes Pro 
 * when a post is saved or updated. 
 * 
 * @author Arun Basil Lal
 * @link https://imageattributespro.com/codex/iaffpro_update_attributes_in_post_by_post_id/
 */
function prefix_iap_update_image_attributes_on_save_post( $post_id ) {

	if ( ! function_exists( 'iaffpro_update_attributes_in_post_by_post_id' ) ) {
		return;
	}

	// post_id from save_post will be the ID of the revision. Finding parent post_id.
	if ( $post_parent_id = wp_get_post_parent_id( $post_id ) ) {
		$post_id = $post_parent_id;
	}

	/**
	 * iaffpro_update_attributes_in_post_by_post_id() will call save_post again.
	 * To prevent this, unhook action right before. 
	 * 
	 * @link https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
	 */
	remove_action( 'save_post', 'prefix_iap_update_image_attributes_on_save_post' );
    
	iaffpro_update_attributes_in_post_by_post_id( $post_id );
}
add_action( 'save_post', 'prefix_iap_update_image_attributes_on_save_post' );