How to get future ID for post which haven’t been created yet?

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.

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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?

  2. 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'
        "
    );
    
  3. 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>";
    
    }