我正在使用RW_Meta_Box 插件依据Rilwis.
我正在使用CPT,添加了一个自定义元框,并删除了title
和editor
.
但我还是想确定标题,原因很明显。
我创建了CPT和meta box,一切都很好,包括all posts
页面使用custom column
挂钩。
当我坐下来为元框中的一个字段设置帖子标题时,我没有得到想要的结果。我试图通过搜索此Q&;来找到我选择的解决方案;一个站点。
最初我尝试:
SOLUTION1:
add_action(\'submitpost_box\', \'hidden_type_title\');
function hidden_type_title() {
global $current_user, $post, $post_type;
global $prefix;
$md = rwmb_meta($prefix . \'name\', array(\'type\' => \'text\'), $post->ID);
if ($post->post_type == \'MY_CPT_NAME\') {
?>
<input type="hidden" name="post_title" value="<?php echo esc_attr(htmlspecialchars($md)); ?>" id="title" />
<?php
} else {
return;
}
}
上面的代码工作正常,但有一个我无法检测到的问题。也就是说,我需要更新两次帖子才能设置帖子标题。因此,我继续尝试save_post
像这样钩住:
SOLUTION2:
add_action(\'save_post\', \'post_updated\');
function post_updated($post_id) {
global $current_user, $post;
if ($post->post_type != \'MY_CPT_NAME\') {
return;
}
global $prefix;
$md = rwmb_meta($prefix . \'name\', array(\'type\' => \'text\'), $post_id);
// verify post is not a revision & not an autosave
if (!wp_is_post_revision($post_id) && !(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)) {
// set the new post title
$post->ID = $post_id;
$post->post_title = $md;
// update the post, removing the action to prevent an infinite loop
remove_action(\'save_post\', \'post_updated\');
wp_update_post($post);
add_action(\'save_post\', \'post_updated\');
return;
}
}
现在,我的处境更糟了,帖子标题立即设置好了,但我的元框数据不知何故没有被保存。第一个解决方案可能有什么问题?