我尝试获取源字段“enddate”的值,将其保存在变量$enddatevar中,并通过激活插件将其写入循环中的目标字段“promote”,但我添加到this working code 不起作用。任何帮助都将不胜感激。
<?php
/*
Plugin Name: Update MetaData for Posts
Description: Enable this plugin to update the metadata for all the posts
Author: JackJohansson
Version: 1.0
Author URI: http://example.com
*/
// Run the loop when the plugin is activated
register_activation_hook(__FILE__, \'update_my_metadata\');
function update_my_metadata(){
$args = array(
\'post_type\' => \'touren\', // Only get the posts
\'post_status\' => \'publish\', // Only the posts that are published
\'posts_per_page\' => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
// Run a loop and update every meta data
//code added to original code
$enddatevar = get_post_meta( $post_id, $key = \'enddate\', $single = false);
//end of code added
//\'meta_value\' used in original code replaced with \'$enddatevar\' below
update_post_meta( $post->ID, \'promote\', \'$enddatevar\' );
}
}
?>
SO网友:Jess_Pinkman
$enddatevar = get_post_meta( $post_id, $key = \'enddate\', $single = false);
未定义$post\\U id,请事先声明,或使用$post->id。
update_post_meta( $post->ID, \'promote\', \'$enddatevar\' );
“$enddatevar”不应在引号内。