ACF_Field_Group::duplicate_postpublicACF 6.1

Duplicates an ACF post.

Метод класса: ACF_Field_Group{}

Хуки из метода

Возвращает

Массив. The new ACF post array.

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

$ACF_Field_Group = new ACF_Field_Group();
$ACF_Field_Group->duplicate_post( $id, $new_post_id );
$id(int|строка)
The ID of the post to duplicate.
$new_post_id(int)
Optional post ID to override.

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

С версии 6.1 Введена.

Код ACF_Field_Group::duplicate_post() ACF 6.4.2

public function duplicate_post( $id = 0, $new_post_id = 0 ) {
	// Disable filters to ensure ACF loads data from DB.
	acf_disable_filters();

	$post = $this->get_post( $id );
	if ( ! $post || ! $post['ID'] ) {
		return false;
	}

	// Get fields before updating field group attributes.
	$fields = acf_get_fields( $post['ID'] );

	// Update attributes.
	$post['ID']  = $new_post_id;
	$post['key'] = uniqid( 'group_' );

	// Add (copy) to title when appropriate.
	if ( ! $new_post_id ) {
		$post['title'] .= ' (' . __( 'copy', 'acf' ) . ')';
	}

	// When importing a new field group, insert a temporary post and set the field group's ID.
	// This allows fields to be updated before the field group (field group ID is needed for field parent setting).
	if ( ! $post['ID'] ) {
		$post['ID'] = wp_insert_post( array( 'post_title' => $post['key'] ) );
	}

	// Duplicate fields and update post.
	acf_duplicate_fields( $fields, $post['ID'] );
	$post = $this->update_post( $post );

	/**
	 * Fires immediately after an ACF post has been duplicated.
	 *
	 * @date 12/02/2014
	 * @since 5.0.0
	 *
	 * @param   array $post The ACF post array.
	 */
	do_action( "acf/duplicate_{$this->hook_name}", $post );

	return $post;
}