update post meta value with other post meta value
Question
I’m a beginner in wordpress / php trying to learn. I want to use the update_post_meta function to populate one meta value (X) with another meta value (Y) everytime Y is changed. I did research and found a code that does this but it only updates the last published post. Can you help me how to modify the code so it works on every post?
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_key' => 'y'
);
$dbResult = new WP_Query($args);
global $post;
if ($dbResult->have_posts()) {
$dbResult->the_post();
$value = get_post_meta($post->ID,'y',true);
}
update_post_meta( $post->ID, 'x', $value) ; ?>
0
10 months
2022-04-01T16:20:05-05:00
2022-04-01T16:20:05-05:00 0 Answers
0 views
0
Leave an answer