acf_connect_attachment_to_post()ACF 5.5.4

acf_connect_attachment_to_post

This function will connect an attacment (image etc) to the post Used to connect attachements uploaded directly to media that have not been attaced to a post

Хуки из функции

Возвращает

true|false. True if attachment was connected.

Использование

acf_connect_attachment_to_post( $attachment_id, $post_id );
$attachment_id(int)
The attachment ID.
$post_id(int)
The post ID.

Список изменений

С версии 5.5.4 Введена.
С версии 5.8.0 Added filter to prevent connection.

Код acf_connect_attachment_to_post() ACF 6.0.4

function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {

	// bail early if $attachment_id is not valid.
	if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
		return false;
	}

	// bail early if $post_id is not valid.
	if ( ! $post_id || ! is_numeric( $post_id ) ) {
		return false;
	}

	/**
	*  Filters whether or not to connect the attachment.
	*
	*  @date    8/11/18
	*  @since   5.8.0
	*
	*  @param   bool $bool Returning false will prevent the connection. Default true.
	*  @param   int $attachment_id The attachment ID.
	*  @param   int $post_id The post ID.
	*/
	if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
		return false;
	}

	// vars
	$post = get_post( $attachment_id );

	// Check if is valid post.
	if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {

		// update
		wp_update_post(
			array(
				'ID'          => $post->ID,
				'post_parent' => $post_id,
			)
		);

		// return
		return true;
	}

	// return
	return true;
}