[wp-hackers] Write a custom field to a new post via JS??

Matthew Gerring mgerring at emrl.com
Wed Jan 6 20:02:51 UTC 2010

I found the temporary ID thing yesterday, and now I have my plugin set up so that it tries to use add_post_meta with the temporary post ID, but all it does is write the meta to the database once the post is published with an ID of 0. It works perfectly as long as the post has been assigned an ID.

I looked through the write_post source, and I see what it's doing but I don't see it correcting the temporary IDs in post_meta. From admin-ajax.php it looks like when you add a custom field, WP creates the real ID and passes it back to the original post, but I'm not sure how to duplicate that functionality in my plugin.

It would be better if I could just hook into the add-meta action in admin-ajax.php, but no matter how I construct the request it always returns -1.

My code is here: http://wordpress.pastebin.com/m728fcfcf. It passes the temporary ID to the server just fine, and returns it back as successful, but WordPress doesn't find the postmeta when I go to save the post.

Anyway. Hoping someone can help.


On Jan 5, 2010, at 3:12 PM, scribu wrote:

> New posts are assigned a temporary id. See how it's used in the
> wp_write_post() function:
> http://xref.yoast.com/trunk/nav.html?wp-admin/includes/post.php.source.html#l439
> Hope that helps.
> On Tue, Jan 5, 2010 at 11:32 PM, Matthew Gerring <mgerring at emrl.com> wrote:
>> Hello all. I have a serious brain twister I hope you all can help me solve.
>> I have an interface in the WordPress "add new post" screen, accessed via
>> the media buttons. Basically what it's supposed to do is write an array to a
>> custom field.
>> It works fine if you're editing a post- I can pass the array to PHP along
>> with the post ID and use add_post_meta(). But I don't know how to do the
>> same thing if you're creating a new post, since there's no post ID to add a
>> custom field to.
>> Can anyone tell me how I would write metadata to a post that hasn't been
>> published yet?
>> -Matthew
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> -- 
> http://scribu.net
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list