2012-04-24 171 views
2

我想添加一个额外的字段在我的添加帖子或添加页面,在那里我插入该字段的值到一个手动添加列添加在wp_posts表中数据库。WordPress - 将额外的列添加到wp_posts,并张贴到它

我知道我可以使用自定义字段模板,但问题是这些自定义字段将值插入wp_postmeta而不是wp_post,并且我需要在同一个表中的单个帖子的所有内容。

任何想法如何做到这一点?

回答

2

如果您已经手动将该字段添加到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); 
+0

函数wp_insert_post_data_filter应该返回数组$ data。 (在版本4.1.1中测试) – mimarcel 2015-03-05 21:40:57