ACF_Field_Group::duplicate_post
Duplicates an ACF post.
Method of the class: ACF_Field_Group{}
Hooks from the method
Returns
Array. The new ACF post array.
Usage
$ACF_Field_Group = new ACF_Field_Group(); $ACF_Field_Group->duplicate_post( $id, $new_post_id );
- $id(int|string)
- The ID of the post to duplicate.
- $new_post_id(int)
- Optional post ID to override.
Changelog
| Since 6.1 | Introduced. |
ACF_Field_Group::duplicate_post() ACF Field Group::duplicate post code ACF 6.8.8
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 duplicating a 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'],
'post_type' => $this->post_type,
)
);
}
// 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;
}