如何加入两个自定义字段值并保存到第三个字段

问题描述 投票:1回答:1

我正在尝试加入两个自定义字段并将组合值保存到第三个自定义字段。

第一个自定义字段有一些选项,如状态缩写(meta key ='state_found')下拉,第二个自定义字段生成随机数,以下是随机数的代码,保存到Post random(Meta key ='post_random')

function get_post_random_wpse( $post_id = 0, $meta_key = 
'post_random',$meta_value = 0 )
{
if( ! ( $post_id > 0 && strlen( $meta_key ) > 0 ) )
return 0;
if( '' === ( $post_rand = get_post_meta( $post_id, $meta_key, true ) ) )
update_post_meta( $post_id, $meta_key, $post_rand = $meta_value);
return $post_rand;
}

我试图将这两个字段与此代码合并

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id, $post) {
$sup = get_post_meta($post_id, 'state_found', true);
$sup = ', ' . get_post_meta($post_id, 'post_random', true);
update_post_meta($post_id, 'new_id', $sup);
}

最终值必须保存到新ID(元键='new_id')

但我不知道为什么事情不起作用随机代码生成完美但价值不会合并,还有一件事我想确保所有随机数必须是唯一的。

谢谢

php wordpress custom-fields wordpress-hook
1个回答
0
投票
add_filter('save_post', 'combine_my_fields');

function combine_my_fields($post_id) {
    $sup = get_post_meta($post_id, 'state_found','test text added');
    $sup.= ', ' . get_post_meta($post_id, 'post_random', 'test text added');
    update_post_meta($post_id, 'new_id', $sup);
}
© www.soinside.com 2019 - 2024. All rights reserved.