admin管理员组

文章数量:1122846

I want to use WP-CLI to create some new posts with custom taxonomy terms assigned. The challenge is that wp_insert_post's tax_input argument only accepts arrays, which I would have to specify on the command line. According to the codex, here is the format required:

$post = array(
    'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies
}

But I need something like this:

wp post create --post_type=lecture --post_title='Test Post #1' --tax-input=[BIG FAT ARRAY]

So my idea was to write a PHP script that executes the WP-CLI command with the array serialized:

//DEFINE VARIABLES
$post_title = "Test Post #1";
$tax_items = array( 9,11,17 );
$tax_input = array( 'course' => $tax_items );

//SERIALIZE THIS ARRAY
$tax_escaped = escapeshellarg(serialize($tax_input));

//WRITE THE COMMAND
$exec_string = 'wp post create --post_type=lecture --post_status=publish --post_title="%1$s" --tax_input=%2$s --porcelain';
$exec_command = sprintf($exec_string, $post_title, $tax_escaped );
$post_id = shell_exec($exec_command);

//THE OUTPUT
//wp post create --post_type=lecture --post_status=publish --post_title="Test Post #1" --tax_input='a:1:{s:5:"class";a:3:{i:0;i:9;i:1;i:11;i:2;i:17;}}' --porcelain

//RELATE THE NEW POST TO THE TAXONOMY TERMS
wp_set_object_terms( $post_id, $tax_items,'course');

Alas, this doesn't work. It creates the new post alright, but it fails to assign the 'course' taxonomy categories I want. Any help would be appreciated.

I know that this overall strategy works, because I succeeded in creating and taxonomizing my posts using wp_insert_posts. So this exercise is for educational purposes and future reference.

I want to use WP-CLI to create some new posts with custom taxonomy terms assigned. The challenge is that wp_insert_post's tax_input argument only accepts arrays, which I would have to specify on the command line. According to the codex, here is the format required:

$post = array(
    'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies
}

But I need something like this:

wp post create --post_type=lecture --post_title='Test Post #1' --tax-input=[BIG FAT ARRAY]

So my idea was to write a PHP script that executes the WP-CLI command with the array serialized:

//DEFINE VARIABLES
$post_title = "Test Post #1";
$tax_items = array( 9,11,17 );
$tax_input = array( 'course' => $tax_items );

//SERIALIZE THIS ARRAY
$tax_escaped = escapeshellarg(serialize($tax_input));

//WRITE THE COMMAND
$exec_string = 'wp post create --post_type=lecture --post_status=publish --post_title="%1$s" --tax_input=%2$s --porcelain';
$exec_command = sprintf($exec_string, $post_title, $tax_escaped );
$post_id = shell_exec($exec_command);

//THE OUTPUT
//wp post create --post_type=lecture --post_status=publish --post_title="Test Post #1" --tax_input='a:1:{s:5:"class";a:3:{i:0;i:9;i:1;i:11;i:2;i:17;}}' --porcelain

//RELATE THE NEW POST TO THE TAXONOMY TERMS
wp_set_object_terms( $post_id, $tax_items,'course');

Alas, this doesn't work. It creates the new post alright, but it fails to assign the 'course' taxonomy categories I want. Any help would be appreciated.

I know that this overall strategy works, because I succeeded in creating and taxonomizing my posts using wp_insert_posts. So this exercise is for educational purposes and future reference.

Share Improve this question edited Aug 16, 2013 at 4:18 Wyck 18k4 gold badges46 silver badges67 bronze badges asked Jun 9, 2013 at 7:44 JP LewJP Lew 3045 silver badges12 bronze badges 1
  • It does not seem to be possible. See the pull request: github.com/wp-cli/entity-command/pull/207 – grappler Commented Sep 3, 2019 at 8:19
Add a comment  | 

4 Answers 4

Reset to default 3

A solution inspired by @Saulo Padilha . <- the only person on the whole internet taking this cr*p seriously

本文标签: wp insert postHow do I pass an array as an argument to a WPCLI command