2
我想添加一個額外的字段在我的添加帖子或添加頁面,在那裏我插入該字段的值到一個手動添加列添加在wp_posts表中數據庫。WordPress - 將額外的列添加到wp_posts,並張貼到它
我知道我可以使用自定義字段模板,但問題是這些自定義字段將值插入wp_postmeta而不是wp_post,並且我需要在同一個表中的單個帖子的所有內容。
任何想法如何做到這一點?
我想添加一個額外的字段在我的添加帖子或添加頁面,在那裏我插入該字段的值到一個手動添加列添加在wp_posts表中數據庫。WordPress - 將額外的列添加到wp_posts,並張貼到它
我知道我可以使用自定義字段模板,但問題是這些自定義字段將值插入wp_postmeta而不是wp_post,並且我需要在同一個表中的單個帖子的所有內容。
任何想法如何做到這一點?
如果您已經手動將該字段添加到wp_posts表中,那麼您只需使用幾個掛鉤將該字段添加到帖子頁面並保存即可。
// Function to register the meta box
function add_meta_boxes_callback($post_type, $post) {
add_meta_box('my_field', 'My Field', 'output_my_meta_box', 'post');
}
add_action('add_meta_boxes', 'add_meta_boxes_callback', 10, 2);
// Function to actually output the meta box
function output_my_meta_box($post) {
echo '<input type="text" name="my_field" value="' . $post->my_field . '" />';
}
// Function to save the field to the DB
function wp_insert_post_data_filter($data, $postarr) {
$data['my_field'] = $_POST['my_field'];
return $data;
}
add_filter('wp_insert_post_data', 'wp_insert_post_data_filter', 10, 2);
函數wp_insert_post_data_filter應該返回數組$ data。 (在版本4.1.1中測試) – mimarcel 2015-03-05 21:40:57