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.

There are several ways to get the id of a post after it has been saved (auto, etc.), but is there a way to get the post id immediately after you create a new post?

I am trying to create a directory using the post id, but I cannot seem to get a static post id.

the code below seems to work but I get an auto incremented id back every time the new post auto saves the draft, I continually get a new number.

function myfunction( $id ) {
        if (!file_exists("/www/foo/blog/wp-content/uploads/" . $id)) {
            mkdir("/www/foo/blog/wp-content/uploads/" . $id, 0777);
        }
}
add_action('save_post', 'myfunction');

I would like to get the post id that it will be saved as. Surely WP has a method for determining this correct? Or does the draft auto save every minute incrementing the id by one until the actual 'publish' button is clicked?

thoughts?

cheers! bo

share|improve this question
    
We already had that Q, but I can't find it. To answer your Q in short: No. How should there an ID, then the ID is the unique identifier number inside the DB? ;) –  kaiser Apr 23 '12 at 19:54
    
Kaiser what do you mean? I cant figure out your second sentence. –  miahelf Apr 23 '12 at 20:28
    
Robert, if the answer posted doesnt solve your problem can you explain why you would expect something other than an auto incremented response? Did you mean the same post when saved again returns a new ID? –  miahelf Apr 23 '12 at 20:33
    
@miahelf correct, if I monitor the uploads folder, everytime it auto saves, the id increments by 1 –  roberthuttinger Apr 23 '12 at 23:49
1  
Yes, you got an ID after saving. And yes, it increments, but it doesn't fill gaps that may occur from deleting posts. And a revision is a post-type of its own with a unique ID. –  kaiser Apr 24 '12 at 0:38
show 1 more comment

1 Answer 1

up vote 7 down vote accepted

Try this...

add_action('post_updated', 'myfunction');

function myfunction( $post_id ) {

    global $post;

        if (!file_exists("/www/foo/blog/wp-content/uploads/" . $post_id)) {
            mkdir("/www/foo/blog/wp-content/uploads/" . $post_id, 0777);
        }
}

NOTE: Change from save_posts to post_updated which will stop the duplicate issue as it fires on "publish" only and not every time you hit add new or update etc.

NOTE: I verified this for you by testing the snippet above - all good.

NOTE: You can also use (wp_upload_dir() . $post_id, 0777) if you want a path that is more transportable or if you are developing a plugin or theme for public use.

share|improve this answer
1  
@roberthuttinger I'm confused, how does this not solve the issue? You want to get the post_id of a new post and create a directory such as - /path/bla/uploads/post-id/ - the above code does exactly that. –  userabuser Apr 24 '12 at 0:01
    
Easy? Are you implying that I somehow might be turning over tables and breaking my keyboard because you won't mark this answer? :) - I'm not that invested believe me so don't read into my message as anything other than a question and a re-statement of an answer which is directly related to your question because your first comment to me is confusing as you say it will work yet doesn't solve the issue yet it directly addresses the issue. My follow up to that is an attempt to gain some clarification that we're on the same page so I can continue to help you until you have solved this problem –  userabuser Apr 24 '12 at 2:32
    
I hope that clears things up for you... Let us know how you get on. –  userabuser Apr 24 '12 at 2:33
    
Had a chance to test it, perfect! The post_updated must have been the correct thing to tap into. Now to do some homework onthe differences between save_post, post_save, and post_updated thanks! –  roberthuttinger Apr 24 '12 at 13:08
1  
save_post and wp_insert_post we're creating folder-ID entries every-time I touched either Add New post or Update or Publish. So simply adding a post then publishing it would create two folders with incremented IDs (one correct and one-incorrect) and similarly if you updated the post you'd get another folder with another ID. post_updated being the only action that committed the official ID once and once only. I'll be looking further into understanding the mechanism behind how WP handles IDs in relation to the former actions too because I don't really get it after this experiment. Enjoy! –  userabuser Apr 24 '12 at 17:50
add comment

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.