I don’t think there is a built in mechanism for searching the meta table for a key
/value
pair other than a full blown post search with WP_Query
. I’d consider that overkill. Just query the table:
$unique = $wpdb->get_var(
"SELECT meta_id
FROM {$wpdb->postmeta}
WHERE meta_key = 'unique_code'
AND meta_value="{$random}"
LIMIT 1"
);
I can’t help but think that more detail in the question might point to a better way to proceed