I have created a custom usermeta field named "designation" and fetched all its meta value in loop but it shows duplicates value. I have tried array_unique() function before loop and inside loop but does not work. Please guide me how can remove duplicates.
<form action="" method="post">
<select name="designation" onchange="if(this.value != 0) && if(this.value != 1){ this.form.submit(); }">
<option value="1">Select Category</option>
<?php
$args = array(
'role' => 'subscriber',
'meta_key' => 'user_designation'
);
$design = array_unique($args);
var_dump($design);
// The Query
$user_query = new WP_User_Query($design);
// User Loop
if ( !empty($user_query->results) ) {
foreach ( $user_query->results as $user ) {
?>
<option value="<?php echo get_user_meta($user->ID, 'user_designation', true); ?>"
<?php if (isset($_POST['designation']) && $_POST['designation'] == "echo get_user_meta($user->ID, 'user_designation', true);") {echo "selected";} ?> >
<?php echo get_user_meta($user->ID, 'user_designation', true); ?>
</option>
<?php
}
} else {
?>
<option value="0">No Category Found</option>
<?php
}
?>
</select>
</form>
Store the user meta in a variable and check if the variable is empty or not, like in below link-