Support

Account

Home Forums General Issues Only Display Custom Field Choices with Posts Reply To: Only Display Custom Field Choices with Posts

  • It will be different depending on if the select field on the post allows a single choice or multiple choices.

    With a custom field you’re going to need to do a WP_Query for every name in the loop. http://codex.wordpress.org/Class_Reference/WP_Query

    
    // select single
    // all of this goes inside your foreach loop
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 1, // only need 1, just checking for any posts with the value
      'meta_query' => array(
        array(
          'key' => 'family_name'
          'value' => $family
        )
      ) 
    )
    $query = new WP_Query($args);
    if (count($query->posts)) {
      echo '<li>' . $family . '</li>';
    )
    
    
    // multiple choices allowed
    // all of this goes inside your foreach loop
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 1, // only need 1, just checking for any posts with the value
      'meta_query' => array(
        array(
          'key' => 'family_name'
          'value' => '"'.$family.'"',
          'compare => 'LIKE'
        )
      ) 
    )
    $query = new WP_Query($args);
    if (count($query->posts)) {
      echo '<li>' . $family . '</li>';
    )
    

    Either way, if there are a lot of choices then this is going to be extremely slow. You would be better off in the long run to use a custom taxonomy, something that’s built right into WP, then you just need to use get_terms() with the right arguments https://developer.wordpress.org/reference/functions/get_terms/