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
1 Answer
Reset to default 1Use 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' );
}