代码之家  ›  专栏  ›  技术社区  ›  warm__tape

使用wp\u insert\u post()创建帖子后获取帖子ID

  •  0
  • warm__tape  · 技术社区  · 7 年前

    似乎找不到确切的答案。我需要在用wp\u insert\u 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

    1 回复  |  直到 7 年前
        1
  •  1
  •   dineshkashera    7 年前

    请将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,
                ),
            );
    
      //  You can also get the new post ID after inserting a new post:
    
                $post_id = wp_insert_post( $log_item , $wp_error ); 
    

    更多帮助: Click Here