我有一个自定义元框,使用WPAlchemy, 我想摆脱的。然而,我想获得所有具有特定自定义元值集的帖子,并将其插入帖子内容的开头,然后删除元值。
我有一种感觉this post 接近我,因为我已经能够成功地将内容添加到帖子的开头,但访问和添加我想要的自定义元值失败。我也有点不确定什么时候该加入WordPress并执行我的操作。
有人对最好的方法有什么建议吗?我想我会构建一个简单的插件,激活它,让它完成它的工作,然后删除它。
我有一个自定义元框,使用WPAlchemy, 我想摆脱的。然而,我想获得所有具有特定自定义元值集的帖子,并将其插入帖子内容的开头,然后删除元值。
我有一种感觉this post 接近我,因为我已经能够成功地将内容添加到帖子的开头,但访问和添加我想要的自定义元值失败。我也有点不确定什么时候该加入WordPress并执行我的操作。
有人对最好的方法有什么建议吗?我想我会构建一个简单的插件,激活它,让它完成它的工作,然后删除它。
激活时像这样的东西怎么样?
function wpa47153_run_once(){
$posts = get_posts(array(\'numberposts\' => -1) );
foreach($posts as $p) :
$meta = get_post_meta($p->ID, \'meta_key\',true);
if($meta) :
$my_post = array();
$my_post[\'ID\'] = $p->ID;
$my_post[\'post_content\'] = $meta . "<br/>" . $p->post_content ;
// Update the post into the database
wp_update_post( $my_post );
unset($my_post);
//remove the meta key
delete_post_meta($p->ID, \'meta_key\');
endif;
endforeach;
}
其中get\\u posts和get\\u post\\u meta根据您的情况进行调整。我宁愿将此作为评论发布到helgatheviking的帖子上,但因为我在这个SE网站上没有声誉,我正在写一篇新帖子。如果管理员觉得没有帮助,可以随意删除它。
Helga的示例非常好用,但您也应该知道,如果您设置了自定义保存处理程序,比如下面这样的内容,检查post屏幕中的元框并保存更新,删除空值,然后如果您不暂时暂停自定义保存处理程序,Helga的脚本将删除所有其他元。例如:
add_action(\'save_post\', \'etm\\save_custom_fields\');
function save_custom_fields( $postId ) {
//avoid accidental duplication
return; //remove this when not using helga\'s function anylonger
if( ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
|| ( defined( \'DOING_AJAX\' ) && DOING_AJAX ) )
return;
//add your custom meta fields here
$fields = array( \'meta-key-1\', \'meta-key-2\', \'...\' );
foreach( $fields as $metaName ):
//keep a flag if it\'s been updated
$new = false;
//get the existing value
$old = get_post_meta( $postId, $metaName, true );
//update the flag with the new value
if ( isset( $_POST[ $metaName ] ) ) {
$new = $_POST[ $metaName ];
}
//add/update or delete the meta
if( $new && $new != $old ) {
update_post_meta( $postId, $metaName, $new, $old ); //also acts to add
} elseif( !$new ) {
delete_post_meta( $postId, $metaName, $old );
}
endforeach;
}
我刚才在一个开发网站上犯了这个错误。谢天谢地,我在运行Helga的脚本之前备份了数据库(它自己可以完美地工作)。但是,由于我可能不是唯一一个使用自定义元脚本/自定义保存处理的人,我认为这是一个重要的提醒。如何制作类似此示例的子帖子http://www.samsung.com/us/mobile/cell-phones/SGH-I717RWAATT概述功能规格库。。。。与链接结构一样。。。。手机/SGH-I717RWAATT-features。。。。手机/SGH-I717RWAATT-specs找不到任何插件或执行此操作的东西!谢谢