使用wp_insert_post()创建后获取帖子ID

时间:2018-10-16 10:50:27

标签: wordpress

似乎无法在任何地方找到明确的答案。使用wp_insert_post()创建帖子后,我需要获取该帖子的ID。

$log_item = array(
    'post_title' => $title,
    'post_content' => '',
    'post_status' => 'publish',
    'post_type' => 'log-item',
    'meta_input' => array(
        // 'wpcf-date-checked' => '',
        // 'wpcf-checked-by' => '',
        'wpcf-belongs-to-id' => $parent_id,
    ),
);
wp_insert_post( $log_item );

之后,如何获取刚刚创建的$log_item帖子的ID?

1 个答案:

答案 0 :(得分:1)

请将帖子ID存储到临时变量中:

     $log_item = array(
            'post_title' => $title,
            'post_content' => '',
            'post_status' => 'publish',
            'post_type' => 'log-item',
            'meta_input' => array(
                // 'wpcf-date-checked' => '',
                // 'wpcf-checked-by' => '',
                'wpcf-belongs-to-id' => $parent_id,
            ),
        );

  //  You can also get the new post ID after inserting a new post:

            $post_id = wp_insert_post( $log_item , $wp_error ); 

更多帮助:Click Here

相关问题