How to update a custom field in all posts with the value of another custom field in the same post?

by Lukas   Last Updated August 13, 2019 22:08 PM

I try to get the value of the source field 'enddate', save it in the variable $enddatevar and write it in the target field 'promote' in the loop by activating the plugin, but the code I added to this working code doesn't work. Any help would be appreciated.

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:
// 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' );

Related Questions

Updated November 10, 2018 15:08 PM

Updated March 01, 2017 14:08 PM

Updated May 27, 2017 00:08 AM

Updated June 27, 2018 03:08 AM

Updated November 10, 2018 10:08 AM