update_post_meta无法使用(自定义帖子类型)

时间:2018-02-01 08:57:16

标签: wordpress

我有自定义的帖子类型叫(书籍)

我尝试在书籍帖子中添加新字段,但是当我将数据添加到字段时,当我点击发布或更新帖子时,它就不会被保存!

我的代码在正常帖子中工作正常,但书籍上没有帖子!

任何人都有解决方案吗?

    add_action("admin_init", "checkbox_init");

function checkbox_init(){
  add_meta_box("checkbox", "book details", "checkbox", "books", "normal", "high");
}

function checkbox(){
  global $post;
  $custom = get_post_custom($post->ID);
  $mycustomfields_bookname = $custom["mycustomfields_bookname "][0];
  $mycustomfields_youtubeid= $custom["mycustomfields_youtubeid"][0];

echo "<div style='text-align:center;'>";
echo "<label>Book Name</label><br /><input style='text-align:center;' type='text' id='mycustomfields_bookname ' name='mycustomfields_bookname' value='". $mycustomfields_bookname."'/>";
echo "<br />";
echo "<label>Youtube Trailer ID</label><br /><input id='mycustomfields_youtubeid' style='text-align:center;' type='text' name='mycustomfields_youtubeid' value='". $mycustomfields_youtubeid."'/>";
echo "</div>";

}

// Save Meta Details
add_action( 'save_post', 'save_metadata', 100);
function save_details(){
  global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post->ID;
}

  update_post_meta($post->ID, "mycustomfields_bookname", $_POST["mycustomfields_bookname"]);
  update_post_meta($post->ID, "mycustomfields_youtubeid", $_POST["mycustomfields_youtubeid"]);  
}

1 个答案:

答案 0 :(得分:0)

NOW 5AM 7AM 9AM 11AM 1PM 3PM 5PM 7PM 9PM 11PM 1AM中,函数名称为add_action( 'save_post', 'save_metadata', 100);,但您的函数名为save_metadata。并且在save_details挂钩中显示参数save_post请勿使用$post

global $post

我认为这应该有用。

带有第一个参数add_action( 'save_post', 'save_details', 100, 3 ); function save_details( $post_id, $post, $update ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post->ID; } update_post_meta( $post->ID, "mycustomfields_bookname", $_POST["mycustomfields_bookname"] ); update_post_meta( $post->ID, "mycustomfields_youtubeid", $_POST["mycustomfields_youtubeid"] ); } 的选项:

$post_id