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.
- 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
4 Answers
Reset to default 3A 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
版权声明:本文标题:wp insert post - How do I pass an array as an argument to a WP-CLI command? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309222a1933939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论