Skip to main content

WP-CLI create post with custom tax

wp mycpt create --tags='test1,test2'

$tags = null;
if ( isset( $assoc_args['tags'] ) ) {
	$tags = wp_parse_list( $assoc_args['tags'] );
	unset( $assoc_args['tags'] );
}

...

$command = "post create --porcelain " . assoc_args_to_str( $assoc_args );
$id = WP_CLI::runcommand( $command, [ 'return' => true ] );
if ( ! (bool) $id ) {
	WP_CLI::error( 'Post not saved!' );
}
wp_set_object_terms( $id, $tags, TAX_BOOKMARK_TAGTAX_MYCPT_TAG );
WP_CLI::success( sprintf( 'Created post %s.', $id ) );