Wordpress if usermeta meta_key exists do this Wordpress if usermeta meta_key exists do this wordpress wordpress

Wordpress if usermeta meta_key exists do this


if ($havemeta) { echo 'your stuff'; }


Wordpress >=3.3

There is a caveat with the proposed solution: a meta_key which value is false will give false negatives.

To ask WordPress if a meta_key is set, even if the value is false, use metadata_exists:

if ( metadata_exists( 'user', $user_id, $meta_key ) ) {   ...}


(TESTED) If you put true as the last argument, according to documentation, it will return the value of the metadata field. Which means you have to put false as last argument.

$havemeta = get_user_meta($userID, 'test', false);

Then, as said in the other answer, you can check it

if ($havemeta)    echo $havemeta;} else {    echo "No";}