如何用同一帖子中另一个自定义域的值更新所有帖子中的自定义域值?

时间:2019-08-13 作者:Lukas

我尝试获取源字段“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\' );
    }
}
?>

2 个回复
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”不应在引号内。

SO网友:Lukas

谢谢我接近解决方案,尝试了两种方法,但没有结合使用。因此

$enddatevar = get_post_meta( $post->ID, $key = \'enddate\', $single = false);
update_post_meta( $post->ID, \'promote\', $enddatevar );
成功了。

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: