Title: Partial Database Export with data specified by a custom WP-CLI command
Author: WordPress VIP Documentation
Published: December 5, 2025

---

 1. [Databases](https://docs.wpvip.com/databases/)
 2. [Partial Database Exports](https://docs.wpvip.com/databases/partial-database-exports/)
 3. Partial Database Export with data specified by a custom WP-CLI command

#  Partial Database Export with data specified by a custom WP-CLI command

The data to be included in a [Partial Database Export](https://docs.wpvip.com/databases/partial-database-exports/)
file can be specified by a [custom WP-CLI command](https://docs.wpvip.com/vip-cli/wp-cli-with-vip-cli/write-custom-wp-cli-commands/)
that is added to an application’s codebase. The logic of the custom WP-CLI command
should be configured to retrieve the desired set of data for the export. The custom
WP-CLI command can also have custom options that help extend the usefulness of the
custom WP-CLI command (e.g. an option that enables a user to retrieve the data by
author or a specific range of time).

For this method to work as expected, the custom WP-CLI command must exist on the
branch that deploys to the environment from which the Partial Database Export file
will be exported.

**Note**

Custom WP-CLI commands should be developed locally and thoroughly tested before 
they are deployed to a VIP Platform environment and used for a Partial Database 
Export.

In this example code for a custom WP-CLI command `my-custom-data-export`, the command
is designed to retrieve a custom set of data. The option `--days` is added to the
custom command to allow a user to limit the data retrieved by a specific number 
of days in the past.

    ```lang-php
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	WP_CLI::add_command( 'my-custom-data-export', function( $args, $assoc_args ) {
    		global $wpdb;

    		$days = WP_CLI\Utils\get_flag_value( $assoc_args, 'days', 30 );

    		$export_data = array(
    			'tables' => array()
    		);

    		$lastUpdatedPostsWhere = "post_date >= NOW() - INTERVAL $days DAY OR post_modified >= NOW() - INTERVAL $days DAY";

    		// wp_posts
    		$export_data['tables']['wp_posts'] = array(
    			'where' => $lastUpdatedPostsWhere
    		);

    		$export_data['tables']['wp_postmeta'] = array(
    			'where' => "meta_id IN (SELECT meta_id FROM wp_postmeta JOIN wp_posts ON (wp_postmeta.post_id = wp_posts.ID) WHERE $lastUpdatedPostsWhere)"
    		);

    		$export_data['tables']['wp_comments'] = array(
    			'where' => "comment_post_ID IN (SELECT comment_post_ID FROM wp_comments JOIN wp_posts ON (wp_comments.comment_post_ID = wp_posts.ID) WHERE $lastUpdatedPostsWhere)"
    		);

    		// wp_users
    		if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'include-users', false ) ) {
    			$export_data['tables']['wp_users'] = array();
    		}

    		// Output JSON
    		WP_CLI::line( json_encode( $export_data, JSON_PRETTY_PRINT ) );
    	} );
    }
    ```

To use the custom WP-CLI command to configure the data of a generated Partial Database
Export file, pass the name of the command within quotes to the `--wpcli-command`
option when running the VIP-CLI command `vip export sql`.

For example:

    ```lang-shell
    // data from the last 30 days
    vip @example-app.develop export sql --wpcli-command="my-custom-data-export"
    ```

To generate a file that includes the data specified by the custom WP-CLI command
but to increase the number of days to 180, pass the name of the command and the 
argument `--days=180` within quotes:

    ```lang-shell
    // data from the last 180 days
    vip @example-app.develop export sql --wpcli-command="my-custom-data-export --days=180"
    ```

Last updated: December 05, 2025