I have a function in function.php and it does wp_insert_post. how can I get the post id which just posted?

when I do wp_insert_posts, it saves a PR value (unique and it saves in post-meta table). Do I need to do "select"? like select post_id from wp-post where oid=".$oid...... etc like this?

I have tried to get
$post_id = wp_insert_post($new_post); $post_id = get_post($post_id) But these are not working.

I am not also very good at wp join. how can I do?

  • I just made it like this.. $newest_post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_type = 'us_visa' ORDER BY post_date DESC LIMIT 1"); is it ok after just did wp-insert-post?
    – pulla
    Aug 17, 2013 at 10:45

1 Answer 1

$post_ID = wp_insert_post($post);

... should work, as you mentioned. From the Codex:

Returns the ID of the post if the post is successfully added to the database. On failure, it returns 0 if $wp_error is set to false, or a WP_Error object if $wp_error is set to true.

  • 1
    it comes "0". I did $post_id = wp_insert_post($new_post); // This will redirect you to the newly created post echo "cc=".$post_id; like this.
    – pulla
    Aug 17, 2013 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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