custom field - Using get_post_meta with new_to_publish

I'm trying to read the custom fields set by the user when publishing a new post:function doSomething($post) {$meta

I'm trying to read the custom fields set by the user when publishing a new post:

function doSomething($post) {

   $meta = get_post_meta($post->ID);

   error_log("post meta: ".print_r($meta, true));

}

add_action("new_to_publish", "doSomething", 999);
add_action("draft_to_publish", "doSomething", 999);
add_action("pending_to_publish", "doSomething", 999);

The custom fields are there for draft_to_publish but not for new_to_publish.

If I use save_post it seems to work every time, but I need it to only run when the status is set to publish for the first time...

I'm trying to read the custom fields set by the user when publishing a new post:

function doSomething($post) {

   $meta = get_post_meta($post->ID);

   error_log("post meta: ".print_r($meta, true));

}

add_action("new_to_publish", "doSomething", 999);
add_action("draft_to_publish", "doSomething", 999);
add_action("pending_to_publish", "doSomething", 999);

The custom fields are there for draft_to_publish but not for new_to_publish.

If I use save_post it seems to work every time, but I need it to only run when the status is set to publish for the first time...

Share Improve this question asked Sep 18, 2012 at 7:49 AndreyuAndreyu 2632 silver badges6 bronze badges 2
  • That's because the fields weren't set then. For e.g. Autosave doesn't save them, etc. Use the values from $_POST instead for your "new_to_publish" action. – kaiser Commented Sep 18, 2012 at 7:53
  • Thanks @kaiser, didn't think to check $_POST. It worked great. – Andreyu Commented Sep 18, 2012 at 8:17
Add a comment  | 

1 Answer 1

Reset to default 4

That's because the fields weren't set then. Note: The »Autosave« process/request also doesn't save them.

Use the values from $_POST instead for your "new_to_publish" action.

EDIT: Do NOT forget to escape and properly sanitize input data! Else you will open a security hole.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744995070a4605139.html

相关推荐

  • custom field - Using get_post_meta with new_to_publish

    I'm trying to read the custom fields set by the user when publishing a new post:function doSomething($post) {$meta

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信