最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - Branch made by several custom values

programmeradmin0浏览0评论

Below code from function.php works fine when the custom value used for conditional branch is only one.

This code means if custom key A is AAA, update custom key B as ZZZ.

if(get_post_meta($post->ID,'MY_CUSTOM_KEY_A',true) == AAA) {
        update_post_meta($post_id, 'MY_CUSTOM_KEY_B', 'ZZZ ');
}

I like to add many custom values into this code. Like, if custom key A is AAA or BBB or CCC or DDD, update key B as ZZZ.

I tried like this, but it failed.

if(get_post_meta($post->ID,'MY_CUSTOM_KEY_A',true) == 'AAA','BBB','CCC','DDD') {
        update_post_meta($post_id, 'MY_CUSTOM_KEY_B', 'ZZZ ');
}

Is it possible to make it ?

Below code from function.php works fine when the custom value used for conditional branch is only one.

This code means if custom key A is AAA, update custom key B as ZZZ.

if(get_post_meta($post->ID,'MY_CUSTOM_KEY_A',true) == AAA) {
        update_post_meta($post_id, 'MY_CUSTOM_KEY_B', 'ZZZ ');
}

I like to add many custom values into this code. Like, if custom key A is AAA or BBB or CCC or DDD, update key B as ZZZ.

I tried like this, but it failed.

if(get_post_meta($post->ID,'MY_CUSTOM_KEY_A',true) == 'AAA','BBB','CCC','DDD') {
        update_post_meta($post_id, 'MY_CUSTOM_KEY_B', 'ZZZ ');
}

Is it possible to make it ?

Share Improve this question asked Jan 23, 2021 at 21:50 cwhirocwhiro 257 bronze badges 1
  • 2 so you wanted: "if A == B or A == C or A == D", and tried: "if A == B, C, D" but it didn't work? Have you tried "if A == B or A == C or A == D"? This isn't a WordPress question, this looks like a PHP question about if statements and comparisons – Tom J Nowell Commented Jan 23, 2021 at 22:29
Add a comment  | 

1 Answer 1

Reset to default 1

Use in_array(), like this:

$search = [ 'AAA', 'BBB', 'CCC', 'DDD' ];
$meta   = get_post_meta( $post->ID, 'MY_CUSTOM_KEY_A', true );

if ( in_array( $meta, $search ) ) {
    update_post_meta( $post->ID, 'MY_CUSTOM_KEY_B', 'ZZZ' );
}
发布评论

评论列表(0)

  1. 暂无评论