Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Calling Post ID: Lets go first with calling post id definition :) This name i actually find inside a core file wp-admin/includes/media.php which means for the post you requested the media upload. Media gets attached to the that post which the media uploader gets called.

Question: Well, for new post (when you click "add new post" or "add new cpt" and you open up wp-admin/post-new.php page. You will notice there is a post id already assigned to the possible new post. To check hover over the media upload button and notice the url.

media upload

So, how to I find the post_id of the possible post? I am trying to find out how to get that post id and searching through core file with no luck.

Appreciate your help :)

Update: Parhaps my question was little missleading. As i did not said where i am going to use the code. I am making a front end user dashboard where I need to get the id not backend admin panel, the image was for demonstration purpose. Going to use the code like on this question Image uploader with "Set Featured Image" link on front end

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You are on the correct path on thinking about post-edit.php.

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L45

See how get_default_post_to_edit is called to return a new post. The second argument tells it to create a post in the database. The function is defined here:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/post.php#L394

Note line 422: $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );

And where would WordPress be without hooks?

do_action('wp_insert_post', $post_ID, $post); can be seen in wp_insert_post's definition here:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/post.php#L2656

Thus, if you hook into is like so:

add_action( 'wp_insert_post', 'wpse_45419_hold_global_post_number', null, 2 );
function wpse_45419_hold_global_post_number( $post_id, $post ) {
    if ( $post->post_status != 'auto-draft' ) return; // not interested, thanks

    // do stuff with $post_id here...
}

This would be quite an interesting thing to have, of course, and is quite surefire. Alternatively, if you look at how the media button gets it:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/media.php#L371

It uses get_upload_iframe_src, defined a little lower, which makes use of the global $post_ID, which brings us back to post-new.php where you can see it being defined on line 46:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L46

So, generally speaking, any hooks that fire off after line 46 are all yours to hook into and tap the $post_ID global, including wp_insert_post, media_buttons, and dozens of others. Note that, init and admin_init come too early up the chain. And make sure the hook is specific enough to fire off only when you need it to. wp_insert_post is good, but will not have the global post_ID set yet, since it's going to be set just after the function returns.

Fascinating, isn't it?

share|improve this answer
    
Great! I just found the function get_upload_iframe_src() :) If save a autodraft when user opens a new post form on front end and use that post id. What do you think? But then I will need to know how to handle it (remove the auto draft) if user don't create a post (close that entry form).. –  Sisir Mar 13 '12 at 14:24
    
The auto-drafts are removed every 7 days core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/… as seen here, there is no need to worry about them, no? –  soulseekah Mar 13 '12 at 14:26
    
If you're going to create a lot of data that you want to erase in case a post is removed look into wp_delete_post, note how any meta information will be automatically removed for the post as well; so you should have no fear of ID collisions, besides in 7 days the next post ID that is issued will probably be far away from the ones deleted and won't be reused. –  soulseekah Mar 13 '12 at 14:30
    
Thanks! That works perfect!! :D Thanks for your time and so much details on your answer :) –  Sisir Mar 13 '12 at 17:55

It comes down to a simple one liner:

var p_id = jQuery("#content-add_media").attr('href').split("post_id=")[1].split("&")[0];

and now p_id holds the post id.

Update In that case you don't need the post id, just keep track of the attachment ids and once you create the post and get its id just update the post_parent field of the attachments to the newly created post idea, something like this

//array of attachment ids
$attachments = array(12,33,434);
global $wpdb;
$wpdb->query(
    "
    UPDATE $wpdb->posts 
    SET post_parent = $post_id
    WHERE ID IN (".implode(",",$attachments) .")
    AND post_type = 'attachment'
    "
);
share|improve this answer
    
sorry, I was little misleading, updated my Q. –  Sisir Mar 13 '12 at 13:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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