Support

Account

Home Forums General Issues How to get the selected values only from a select field? Reply To: How to get the selected values only from a select field?

  • You need to do a query of all of the posts where this field is used and then loop through all of the posts to collect a list of the values that are actually used. The only place that information is stored is in the _postmeta table related to each post.

    For example:

    
    global $post;
    $cities = array();
    $args = array(
      'post_type' => 'page',
      'posts_per_page' => -1
    )
    $query = new WP_Query($args);
    if ($query->have_posts()) {
      while ($query->have_posts()) {
        $query->the_post();
        $city= get_field('city-field-name');
        if (!in_array($cities)) {
          $cities[] = $city;
        }
      }
    }