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:

'post_type' => 'lejlighed'

while (have_posts()):

$meta = get_post_meta($post->ID, 'antal_varelser');

foreach ($meta as $m) {

    echo '<option value="1">' . $m . '</option>';


The result: 2 2 3 4 3 3 4

Any suggestions?

  • 3
    Firstly, this isn't a WordPress specific question, so I'm going to close vote it, ask PHP specific questions on Stack Overflow. Secondly, don't use query_posts, you can find the explanation for the why on here and here. Thirdly, to help you out anyway, use array_unique to have only unique values. – Nicolai Aug 11 '14 at 15:28
  • Ok - If you had read my question i stated that I already have tried using ´array_unique´. I will post this on stackoverflow. And good explanation on why i shouldn't use query_posts - really helpful... – TietjeDK Aug 11 '14 at 15:32
  • Added the links to the threads that are explaining it. Sorry, skipped the array_unique part in you question somehow, but it actually changes nothing about my suggestion. Not sure why it isn't working for you. – Nicolai Aug 11 '14 at 15:35
  • Thank you. Had a look at query_posts and you are right. Got some good feedback on stackoverflow. – TietjeDK Aug 11 '14 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.