I'm trying to loop through a custom post type and display all meta values for a meta key.
I'm able to display all the values, however there are some duplicates. I want to remove those duplicates.
I have tried using array_unique
without luck.
My code:
<?php
query_posts(array(
'post_type' => 'lejlighed'
));
?>
<?php
while (have_posts()):
the_post();
$meta = get_post_meta($post->ID, 'antal_varelser');
foreach ($meta as $m) {
echo '<option value="1">' . $m . '</option>';
}
endwhile;
?>
The result: 2 2 3 4 3 3 4
My code with array_unique
:
<?php
$the_query = new WP_Query(array(
'post_type' => 'lejlighed',
));
?>
<?php
while ($the_query->have_posts()):
$the_query->the_post();
$meta = get_post_meta($post->ID, 'antal_varelser');
$result = array_unique($meta);
foreach($result as $r)
{
echo '<option value="1">' . $m . '</option>';
}
endwhile; ?>
Outputs nothing.
print_r($meta)
returns:
Array ( [0] => 2 ) Array ( [0] => 12 ) Array ( [0] => 4 ) Array ( [0] => 2 ) Array ( [0] => 5 ) Array ( [0] => 2 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 3 ) Array ( [0] => 3 )
Any suggestions?