wordpress get_post_meta check if multiple values set

I’ve got a number of custom fields in a WP theme, and I want to check if they have values – but would like a short cut to check multiple values at once – something like this:

if ( get_post_meta ( $post->ID, "first_value", "second_value", "third_value", $single = true) !="") :

    // do stuff here, as they are all set ##

else:

    // do something else, as they are not all set ##

endif;

this does not throw an error, but it only checks if the first value is set – any ideas?

Related posts

Leave a Reply

1 comment

  1. First note the affects of setting the $single variable to true:

    If $single is set to false, or left
    blank, the function returns an array
    containing all values of the specified
    key. If $single is set to true, the
    function returns the first value of
    the specified key as a string, thus you can use string compare (not in an array)

    Solution 1: That being said you could then use $single=false to get an array of values, and then compare the array of values to an array of null values.

    Solution 2: Or you could use several conditions in the if_else statement:

    if ( get_post_meta($post->ID,"first_value",true)!="" && get_post_meta($post->ID,"second_value",true)!="" && get_post_meta($post->ID,"third_value",true)!="") :
    
        // do stuff here, as they are all set ##
    
    else:
    
        // do something else, as they are not all set ##
    
    endif;
    

    Solution 3: You could also use nested if_then statements if you prefer those.

    The question is, which solution works best for you?