ACF\CLI

JsonCommand::exportpublicACF 6.8

Exports field groups, post types, taxonomies, and options pages to a JSON file.

Exports ACF items to a JSON file, replicating the functionality of the export tool in the WordPress admin.

OPTIONS

[--field-groups=<keys>]
Export specific field groups by key or label, comma separated.
[--post-types=<keys>]
Export specific post types by key or label, comma separated.
[--taxonomies=<keys>]
Export specific taxonomies by key or label, comma separated.
[--options-pages=<keys>]
Export specific options pages by key or label, comma separated. Requires ACF PRO.
[--dir=<directory>]
Directory path to write the JSON file to.
[--stdout]
Print the JSON to stdout instead of writing to a file.

EXAMPLES

# Export all items to a directory
$ wp acf json export --dir=./exports/
# Export specific field groups by key
$ wp acf json export --field-groups=group_abc123,group_def456 --dir=./
# Export a field group by label
$ wp acf json export --field-groups="My Field Group" --dir=./
# Export mixed items (field groups and post types)
$ wp acf json export --field-groups=group_abc --post-types=post_type_def --dir=./
# Export to stdout for piping
$ wp acf json export --stdout
$ wp acf json export --field-groups=group_abc123 --stdout | jq .

Method of the class: JsonCommand{}

No Hooks.

Returns

null. Nothing (null).

Usage

$JsonCommand = new JsonCommand();
$JsonCommand->export( $args, $assoc_args );
$args(array) (required)
Positional arguments.
$assoc_args(array) (required)
Associative arguments.

Changelog

Since 6.8 Introduced.

JsonCommand::export() code ACF 6.8.8

public function export( $args, $assoc_args ) {
	$this->log_command( 'export' );

	$field_groups_arg  = get_flag_value( $assoc_args, 'field-groups' );
	$post_types_arg    = get_flag_value( $assoc_args, 'post-types' );
	$taxonomies_arg    = get_flag_value( $assoc_args, 'taxonomies' );
	$options_pages_arg = get_flag_value( $assoc_args, 'options-pages' );
	$output_dir        = get_flag_value( $assoc_args, 'dir' );
	$stdout            = get_flag_value( $assoc_args, 'stdout', false );

	if ( ! $output_dir && ! $stdout ) {
		WP_CLI::error( 'You must specify --dir=<directory> or --stdout.' );
	}

	if ( $output_dir && $stdout ) {
		WP_CLI::error( 'Cannot specify both --dir and --stdout.' );
	}

	if ( $output_dir && ! is_dir( $output_dir ) ) {
		WP_CLI::error( sprintf( 'Directory not found: %s', $output_dir ) );
	}

	if ( $output_dir && ! wp_is_writable( $output_dir ) ) {
		WP_CLI::error( sprintf( 'Directory is not writable: %s', $output_dir ) );
	}

	$keys = $this->resolve_export_keys( $field_groups_arg, $post_types_arg, $taxonomies_arg, $options_pages_arg );

	if ( empty( $keys ) ) {
		WP_CLI::error( 'No items found to export.' );
	}

	$json = array();

	foreach ( $keys as $key ) {
		$post_type = acf_determine_internal_post_type( $key );
		$post      = acf_get_internal_post_type( $key, $post_type );

		if ( empty( $post ) ) {
			WP_CLI::warning( sprintf( "Item not found for key '%s'. Skipping.", $key ) );
			continue;
		}

		if ( 'acf-field-group' === $post_type ) {
			$post['fields'] = acf_get_fields( $post );
		}

		$post   = acf_prepare_internal_post_type_for_export( $post, $post_type );
		$json[] = $post;
	}

	if ( empty( $json ) ) {
		WP_CLI::error( 'No items could be exported.' );
	}

	$encoded = acf_json_encode( $json );

	if ( $stdout ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $encoded . "\n";
		return;
	}

	$file_name = 'acf-export-' . date( 'Y-m-d' ) . '.json';
	$file_path = trailingslashit( $output_dir ) . $file_name;

	// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
	$result = file_put_contents( $file_path, $encoded . "\r\n" );

	if ( false === $result ) {
		WP_CLI::error( sprintf( 'Failed to write to %s', $file_path ) );
	}

	WP_CLI::success( sprintf( 'Exported %d item(s) to %s', count( $json ), $file_path ) );
}