ACF\CLI
JsonCommand::import
Imports field groups, post types, taxonomies, and options pages from a JSON file.
Reads an ACF export JSON file and imports the items into the database, replicating the functionality of the import UI in the WordPress admin. If an item with the same key already exists, it will be updated. Options pages require ACF PRO.
OPTIONS
- <file>
- Path to the JSON file to import.
EXAMPLES
# Import field groups, post types, taxonomies, and options pages from a file $ wp acf json import ./acf-export-2025-01-01.json Imported field-group: My Field Group (group_abc123) Imported post-type: Book (post_type_def456) Success: Imported 2 item(s).
# Import a single field group JSON file $ wp acf json import ./group_abc123.json
# Re-import to update existing items $ wp acf json import ./acf-export.json Updated field-group: My Field Group (group_abc123) Success: Imported 1 item(s).
Метод класса: JsonCommand{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$JsonCommand = new JsonCommand(); $JsonCommand->import( $args, $assoc_args );
- $args(массив) (обязательный)
- Positional arguments.
- $assoc_args(массив) (обязательный)
- Associative arguments.
Список изменений
| С версии 6.8 | Введена. |
Код JsonCommand::import() JsonCommand::import ACF 6.8.8
public function import( $args, $assoc_args ) {
$this->log_command( 'import' );
if ( empty( $args[0] ) ) {
WP_CLI::error(
"Missing required file argument.\n\n" .
"Usage: wp acf json import <file>\n\n" .
"Example:\n" .
" wp acf json import ./acf-export.json\n\n" .
"See: wp help acf json import"
);
}
$file_path = $args[0];
if ( ! file_exists( $file_path ) ) {
WP_CLI::error( sprintf( 'File not found: %s', $file_path ) );
}
if ( 'json' !== pathinfo( $file_path, PATHINFO_EXTENSION ) ) {
WP_CLI::error( 'File must have .json extension.' );
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$json = file_get_contents( $file_path );
$json = json_decode( $json, true );
if ( ! $json || ! is_array( $json ) ) {
WP_CLI::error( 'Import file is empty or contains invalid JSON.' );
}
// Normalize single item to array (matches admin UI behavior).
if ( isset( $json['key'] ) ) {
$json = array( $json );
}
$ids = array();
foreach ( $json as $to_import ) {
if ( ! is_array( $to_import ) ) {
WP_CLI::warning( 'Skipping invalid item (expected array, got ' . gettype( $to_import ) . ').' );
continue;
}
if ( empty( $to_import['key'] ) ) {
WP_CLI::warning( 'Skipping item with no key.' );
continue;
}
$post_type = acf_determine_internal_post_type( $to_import['key'] );
if ( ! $post_type ) {
WP_CLI::warning( sprintf( "Could not determine post type for key '%s'. Skipping.", $to_import['key'] ) );
continue;
}
$post = acf_get_internal_post_type_post( $to_import['key'], $post_type );
if ( $post ) {
$to_import['ID'] = $post->ID;
}
$result = acf_import_internal_post_type( $to_import, $post_type );
if ( empty( $result ) || ! isset( $result['ID'] ) ) {
WP_CLI::warning( sprintf( "Failed to import item with key '%s'.", $to_import['key'] ) );
continue;
}
$action = ! empty( $to_import['ID'] ) ? 'Updated' : 'Imported';
$title = ! empty( $result['title'] ) ? $result['title'] : $to_import['key'];
$type_label = $this->get_type_label( $post_type );
WP_CLI::log( sprintf( '%s %s: %s (%s)', $action, $type_label, $title, $to_import['key'] ) );
$ids[] = $result['ID'];
}
if ( empty( $ids ) ) {
WP_CLI::warning( 'No items were imported.' );
return;
}
WP_CLI::success( sprintf( 'Imported %d item(s).', count( $ids ) ) );
}