admin管理员组

文章数量:1323714

$new_post = array(
                        'post_title'    =>      $title,
                        'post_content'  =>      $description,
                        'post_category' =>      array($_POST['cat']),  // Usable for custom taxonomies too
                        'tags_input'    =>      array($tags),
                        'post_status'   =>      'publish',           // Choose: publish, preview, future, draft, etc.
                        'post_type'     =>      'my_custom_type'
                );
wp_insert_post($new_post);

How can I get the post id? Is it automatically generated? How can I show it before the form is posted? I'm trying to create a frontend form where I show to the user the post id is going to be created. Like "Hey man, you're posting the article nr # <?php echo $postID;?>". Is there a way or I'm totally out of mind? Thanks in advance.

$new_post = array(
                        'post_title'    =>      $title,
                        'post_content'  =>      $description,
                        'post_category' =>      array($_POST['cat']),  // Usable for custom taxonomies too
                        'tags_input'    =>      array($tags),
                        'post_status'   =>      'publish',           // Choose: publish, preview, future, draft, etc.
                        'post_type'     =>      'my_custom_type'
                );
wp_insert_post($new_post);

How can I get the post id? Is it automatically generated? How can I show it before the form is posted? I'm trying to create a frontend form where I show to the user the post id is going to be created. Like "Hey man, you're posting the article nr # <?php echo $postID;?>". Is there a way or I'm totally out of mind? Thanks in advance.

Share Improve this question edited Apr 20, 2016 at 12:50 TheDeadMedic 36.7k9 gold badges68 silver badges102 bronze badges asked Apr 20, 2016 at 10:51 KleeiaKleeia 1071 gold badge2 silver badges9 bronze badges 4
  • You will need run a separate query to get the last ID from the wp_posts table. The new post will have this ID, but be careful, another process running at the same time might intercept this ID and save a draft or attachment under it, so the new post might have another ID to the one you are displaying. There is just no reliable way to know a post ID before hand. You'll need to remember, all attachments, drafts, revisions and auto drafts are saved in wp_posts, so no two published posts will have ID's with a difference of one. You'll need to re-assess your approach – Pieter Goosen Commented Apr 20, 2016 at 11:07
  • @PieterGoosen Thank you for your answer! So basically, I should display the post id after the post is published, is it correct? – Kleeia Commented Apr 20, 2016 at 11:21
  • 1 wp_insert_post returns the ID of the new post when called in the form $id = wp_insert_post( $new_post );. Maybe you can use that fact to save the post as a draft as soon as the user starts typing and then call wp_update_post when he submits the form, updating the post status to publish. – Luis Sanz Commented Apr 20, 2016 at 11:37
  • @LuisSanz Thanks a lot for this. I'll work on this workaround. Great! – Kleeia Commented Apr 20, 2016 at 11:59
Add a comment  | 

2 Answers 2

Reset to default 10

You'll have to do this in two steps. First, you will create a post in the draft mode, using wp_insert_post(). The wp_insert_post itself will return to you the ID of the inserted post:

<?php
$new_post = array(
    'post_title' => 'Draft title',
    'post_status'   => 'draft'
    'post_type'     =>      'my_custom_type'
);
$postId = wp_insert_post($new_post);
?>

<form method="post" action="your-action.php">
    <p>Hey! You are creating the post #<?php echo $postId; ?></p>
    <input type="hidden" name="draft_id" value="<?php echo $postId; ?>">
    ...
</form>

After that, in the action page, you will get the draft id and update the post. You'll use wp_update_post informing the draft ID.

<?php
$draftId = $_POST['draft_id'];
...

$updated_post = array(
    'ID'            =>      $draftId,
    'post_title'    =>      $title,
    ...
    'post_status'   =>      'publish', // Now it's public
    'post_type'     =>      'my_custom_type'
);
wp_update_post($updated_post);
?>

Hope it helps :)

Check the documentation:

Return: (int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.

Thus:

$result = wp_insert_post( $data );

if ( $result && ! is_wp_error( $result ) ) {
    $post_id = $result;
    // Do something else
}

本文标签: formsGet post ID from wpinsertpost()