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.
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
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 inwp_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:
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 topost-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, includingwp_insert_post
,media_buttons
, and dozens of others. Note that,init
andadmin_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 globalpost_ID
set yet, since it’s going to be set just after the function returns.Fascinating, isn’t it?
It comes down to a simple one liner:
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 thisI came across this post looking for a way to get the next post id without creating a new post like when you are adding a new post/page which goes through post-new.php . Here’s an example code I created for the next ID of a woocommerce post type
'shop_order'
but it does not create a new post/order. It’s the same function as when you’re adding a new page via post-new.php . This shortcode[myget_default_order_page_to_edit]
will show the next ID number on the frontend. Past shortcode inside page. Paste the below code in your child theme functions.php or create it in code snippets plugin.