2
$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.

  • 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 Apr 20 '16 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 Apr 20 '16 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 Apr 20 '16 at 11:37
  • @LuisSanz Thanks a lot for this. I'll work on this workaround. Great! – Kleeia Apr 20 '16 at 11:59
4

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_draft_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 :)

  • 1
    This is very cool!! Thank you @André I was just looking something like this! – Kleeia Apr 20 '16 at 14:05
  • Welcome, @Kleeia ;) – André Gumieri Apr 20 '16 at 14:08
3

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.