WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

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

@type    function
Хуки из функции

Возвращает

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 5.10.2

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

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

	// Bail ealry 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;
}