我正在尝试使用wp_insert_post
. 我希望所有的帖子都是第一个帖子的家长。
我尝试了不同的方法,但没有成功。有人知道怎么做吗?
foreach ( $books as $book ) {
$post = array(
\'post_title\' => esc_html__( \'Post\', \'book-cpt\' ),
\'post_content\' => \'test\',
\'post_status\' => \'publish\',
\'post_type\' => \'book\',
);
$book_id = wp_insert_post( $post );
};
最合适的回答,由SO网友:Picard 整理而成
如果我理解正确,这应该可以:
$first_post = true;
$post_parent = 0;
foreach ( $books as $book ) {
$post = array(
\'post_title\' => esc_html__( \'Post\', \'book-cpt\' ),
\'post_content\' => \'test\',
\'post_status\' => \'publish\',
\'post_type\' => \'book\',
\'post_parent\' => $post_parent
);
$book_id = wp_insert_post( $post );
if ($first_post) {
$post_parent = $book_id;
$first_post = false;
}
};