我已经创建了一个带有2个自定义字段的自定义帖子类型。。。当我创建这种类型的帖子并填写自定义字段时,数据实际上会保存到db。。。但是,如果我丢弃其中一个,那么该帖子的自定义字段数据将从数据库中删除。
我以为垃圾处理基本上就是把post\\u状态改为“垃圾”,所以除非你真的永久删除它,否则它就看不见了?
如果是这样的话,为什么当我丢弃一个项目时会丢失自定义字段数据?
以下是上述问题的所有相关代码:
<?php
add_action(\'admin_init\', \'wpg_add_testimonial_author\');
function wpg_add_testimonial_author() {
add_meta_box(\'wpg_testimonial_author\', __(\'Author Information\', \'quotable\'), \'wpg_testimonial_author\', \'testimonials\', \'side\', \'low\');
}
function wpg_testimonial_author() {
global $post;
$custom = get_post_custom($post->ID);
$testimonial_author_name = $custom[\'testimonial_author_name\'][0];
$testimonial_author_link = $custom[\'testimonial_author_link\'][0];
?>
<p>
<label><?php _e("Author\'s Name:", \'quotable\'); ?></label>
<input name="testimonial_author_name" value="<?php echo $testimonial_author_name; ?>" />
</p>
<p>
<label><?php _e(\'Attribution Link:\', \'quotable\'); ?></label>
<input name="testimonial_author_link" value="<?php echo $testimonial_author_link; ?>" />
</p>
<?php
}
function wpg_save_testimonial_author() {
global $post;
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
if (defined(\'DOING_AJAX\')) {
return;
}
update_post_meta($post->ID, \'testimonial_author_name\', $_POST[\'testimonial_author_name\']);
update_post_meta($post->ID, \'testimonial_author_link\', $_POST[\'testimonial_author_link\']);
}
add_action(\'save_post\', \'wpg_save_testimonial_author\');
function wpg_row_actions() {
global $post;
if($post->post_type == \'page\') {
if(!current_user_can(\'edit_page\')) {
return;
}
}
else {
if(!current_user_can(\'edit_post\')) {
return;
}
}
if($post->post_status == \'trash\') {
$actionLinks = \'<div class="row-actions"><span class="untrash"><a title="\'.__(\'Restore this item\', \'quotable\').\'" href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=untrash\', \'untrash-\'.$post->post_type.\'_\'.$post->ID).\'">\'.__(\'Restore\', \'quotable\').\'</a> | </span>\';
$actionLinks .= \'<span class="trash"><a href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=delete\', \'delete-\'.$post->post_type.\'_\'.$post->ID).\'" title="\'.__(\'Delete this item permanently\', \'quotable\').\'" class="submitdelete">\'.__(\'Delete Permanently\', \'quotable\').\'</a></span>\';
}
else {
$actionLinks = \'<div class="row-actions"><span class="edit"><a title="\'.__(\'Edit this item\', \'quotable\').\'" href="\'.get_admin_url().\'post.php?post=\'.$post->ID.\'&action=edit">\'.__(\'Edit\', \'quotable\').\'</a> | </span>\';
$actionLinks .= \'<span class="inline hide-if-no-js"><a title="\'.__(\'Edit this item inline\', \'quotable\').\'" class="editinline" href="#">\'.__(\'Quick Edit\', \'quotable\').\'</a> | </span>\';
$actionLinks .= \'<span class="trash"><a href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=trash\', \'trash-\'.$post->post_type.\'_\'.$post->ID).\'" title="\'.__(\'Move this item to the Trash\', \'quotable\').\'" class="submitdelete">\'._x(\'Trash\', \'verb (ie. trash this post)\', \'quotable\').\'</a></span>\';
}
return $actionLinks;
}
function wpg_edit_testimonials_columns($columns) {
$columns = array(
\'cb\' => \'<input type="checkbox" />\',
\'testimonial_author\' => __(\'Author\', \'quotable\'),
\'testimonial_text\' => __(\'Testimonial\', \'quotable\'),
\'attribution_link\' => __(\'Attribution Link\', \'quotable\'),
\'date\' => __(\'Date Added\', \'quotable\')
);
return $columns;
}
function wpg_manage_testimonials_columns($column, $post_id) {
global $post;
$custom = get_post_custom($post->ID);
$testimonial_author_name = $custom[\'testimonial_author_name\'][0];
$testimonial_author_link = $custom[\'testimonial_author_link\'][0];
$wpg_row_actions = wpg_row_actions();
switch($column) {
case \'testimonial_author\':
echo $testimonial_author_name.$wpg_row_actions;
break;
case \'testimonial_text\':
echo $post->post_content;
break;
case \'attribution_link\':
echo $testimonial_author_link;
break;
default :
break;
}
}
add_filter(\'manage_edit-testimonials_columns\', \'wpg_edit_testimonials_columns\');
add_action(\'manage_testimonials_posts_custom_column\', \'wpg_manage_testimonials_columns\', 10, 2);
自定义帖子类型不支持帖子的“标题”部分,因此它不作为编辑帖子页面中的一列包含。因此,我不得不修改我自己版本的row\\u actions()函数,以便在每个帖子悬停时启用编辑/快速编辑/垃圾箱链接。
所有功能都正常工作。。。除了将帖子移动到垃圾箱时,会从数据库中删除“推荐作者”和“归属链接”值。不知道为什么。。。