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 ?

  • 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
    Jan 23, 2021 at 22:29

1 Answer 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' );
  • Hi fuxia Thank you so much for your beautiful code. It works exactly as I expected. I changed "$post->" to "$post->ID".
    – cwhiro
    Jan 24, 2021 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.