admin管理员组文章数量:1415645
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.
<?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:
*/
// 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' );
}
}
?>
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.
<?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
*/
// 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' );
}
}
?>
Share
Improve this question
asked Aug 13, 2019 at 21:35
LukasLukas
1
2 Answers
Reset to default 1$enddatevar = get_post_meta( $post_id, $key = 'enddate', $single = false);
$post_id is not defined, either declare it beforehand, or use $post->ID.
update_post_meta( $post->ID, 'promote', '$enddatevar' );
'$enddatevar' shouldnt be inside quotes.
Thanks! I was close to the solution, tried both, but not in combination. Thus
$enddatevar = get_post_meta( $post->ID, $key = 'enddate', $single = false);
update_post_meta( $post->ID, 'promote', $enddatevar );
worked.
本文标签: How to update a custom field in all posts with the value of another custom field in the same post
版权声明:本文标题:How to update a custom field in all posts with the value of another custom field in the same post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745243394a2649445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论