1

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

3 Answers 3

3

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?

4
  • 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, 2012 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, 2012 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, 2012 at 14:30
  • Thanks! That works perfect!! :D Thanks for your time and so much details on your answer :)
    – Sisir
    Mar 13, 2012 at 17:55
1

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'
    "
);
1
  • sorry, I was little misleading, updated my Q.
    – Sisir
    Mar 13, 2012 at 13:32
0

I 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.

/**
 * Get the default page information to use.
 *
 * @since 2.5.0
 * @deprecated 3.5.0
 * @deprecated Use get_default_post_to_edit()
 *
 * Helpful examples: https://hotexamples.com/examples/-/-/get_default_post_to_edit/php-get_default_post_to_edit-   function-examples.html
 * https://developer.wordpress.org/reference/functions/get_default_post_to_edit/
 * @return WP_Post Post object containing all the default post data as attributes
 * shop_order is a post type from Woocommerce. Replace shop_order with desired post type.
 * shortcode to view the next id/order # [myget_default_order_page_to_edit]
 */
add_shortcode('myget_default_order_page_to_edit','myget_default_order_page_to_edit');
function myget_default_page_to_edit( $post_type = 'shop_order'){
    
    require_once( ABSPATH . '/wp-admin/includes/post.php' );

     $order = get_default_post_to_edit($post_type, true);

     echo $order->ID;
     
// echo post id/ order id inside html
    echo "<div>The Next Order ID # {$order->ID}</div>";

}
3
  • Note that shortcode callbacks have to return a string. They must not use echo or print, because that would be printed directly when the post content is parsed for shortcodes, not when it's printed to the page.
    – fuxia
    Dec 9, 2021 at 8:22
  • I used this to develop a "add new" button linked to the permalink where the shortcode is located. I actually didn't know about not using print/ echo in a shortcode. The above code works for me but I could update it to change echo to return. I created a shortcode so that I could easily test and view it on the frontend. Since the author was using it for a Frontend Dashboard. If I make the changes would it be a better solution? Dec 9, 2021 at 23:38
  • Thank you for letting me know by the way so I can improve my skills. Dec 10, 2021 at 0:15

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.