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.
2 Answers
Reset to default 10You'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()
版权声明:本文标题:forms - Get post ID from wp_insert_post() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128135a2422035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 inwp_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:07wp_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 callwp_update_post
when he submits the form, updating the post status to publish. – Luis Sanz Commented Apr 20, 2016 at 11:37