WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use the code to get all the meta boxes values in a custom type

<?php
$args = array('post_type' => 'posttype');
$emptyvalue = "";
$optionname = "optionname";
$the_query = new WP_Query($args);

$output ="<select name='".$optionname."'> <option value='".$emptyvalue."'>Location</option>'";

while ( $the_query->have_posts() ) : $the_query->next_post();

    $id= $the_query->post->ID;

    $location = get_post_meta($id, 'institution_location', true);

    $get_location = $_GET['institution-location'];

    $selected = ($get_location == $location) ? 'selected=selected' : '';

    $output .="<option $selected name='".$location."' value='".$location."'>".$location."</option>";

endwhile;
$output .="</select>";

echo $output;

?>

But if there are two custom post have the same values it returns the two values, so I want to just return one value of them.

Thanks

share|improve this question
up vote 0 down vote accepted

Solution

I created array outside the while loop and checked if the value is in array or not.

<?php
  $args = array('post_type' => 'posttype');
  $emptyvalue = "";
  $optionname = "optionname";
  $the_query = new WP_Query($args);

  $output ="<select name='".$optionname."'> <option                                                                  value='".$emptyvalue."'>Location</option>'";

  $arr = array();
  while ( $the_query->have_posts() ) : $the_query->next_post();

  $id= $the_query->post->ID;

  $location = get_post_meta($id, 'institution_location', true);

  if(!in_array($location, $arr)){
  array_push($arr, $location);
  $get_location = $_GET['institution-location'];
  $selected = ($get_location == $location) ? 'selected=selected' : '';
  $output .="<option $selected name='".$location."' value='".$location."'>".$location."   </option>";
  }

  endwhile;

   $output .="</select>";

  echo $output;

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.