Support

Account

Home Forums Front-end Issues Show an entire specific field group in frontend

Solved

Show an entire specific field group in frontend

  • I may have missed this in the documentation, but is there a way to show all the fields in an exact field group?
    For example, i have a group called ‘Restaurant Details’, which has a postID of 12345.

    How can I call this entire group in the frontend?

    I’ve tried code like:

    <?php 
            $groupID = '12345';
            $fields = get_fields($groupID);
    
              $fields = get_field_objects();
    
              if( $fields )
              {
                foreach( $fields as $field_name => $field )
                {
                  if( $field['value'] )
                  {
                      echo '<dl>';
                        echo '<dt>' . $field['label'] . '</dt>';
                        echo '<dd>' . $field['value'] . '</dd>';
                      echo '</dl>';
                    }
                }
              }
    
            ?>

    But it shows all fields associated with that post, not just that one field group. Can I call a specific group for front end, instead of each field individually, or instead of all fields?
    THe overall problem is that there is an admin only field group that comes up, as well as an address group that I don’t want to show in this dump of all fields…

    Any thoughts?

  • Hi @aaronrobb

    Thanks for the question.

    You can use a core filter (yet to be documented) to load a field group’s fields.

    Here is an example:

    
    <?php 
    
    $group_ID = 123;
    
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
    if( $fields )
    {
    	foreach( $fields as $field )
    	{
    		$value = get_field( $field['name'] );
    		
    		echo '<dl>';
    			echo '<dt>' . $field['label'] . '</dt>';
    			echo '<dd>' .$value . '</dd>';
    		echo '</dl>';
    	}
    }
    
     ?>
    

    Good luck!

    Thanks
    E

  • Ok, but this won’t work with the code filtering out any fields that are not filled in, right?
    That’s the
    if( $field['value'] )

    part from the original code above…

  • Hi @aaronrobb

    Just change if( $field['value'] ) to if( $value )

  • Brilliant, that works!

    Ok, so is there a way to call more than one group ID? Lets say i want to show all of group 123 and group 345 together? Do i just have to repeat this entire code for each?

  • Nope, you can merge arrays like so

    <?php 
    
    $fields1 = apply_filters('acf/field_group/get_fields', array(), $group_ID1);
    $fields2 = apply_filters('acf/field_group/get_fields', array(), $group_ID2);
    
    $fields = array_merge( $fields1, $fields2 );
    
     ?>
    
  • Hi, i can’t create new thread so I hope anyone would help me 🙂

    I want to include post editor to my front-end page. I’m using advanced custom field for word press. http://www.advancedcustomfields.com/resources

    I have following group for example:
    -group1
    ->price
    ->option
    ->optio2

    When I’m using the following code:
    http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/
    I’m getting all fields which I can change, but I need to edit only field ‘option’, the rest of field are unnecessary.

    What should I do? I dont have idea, I’m working on this from three hours.

  • Hi @jack_sparrow

    Sadly, at the moment, there is no way to customize the exact fields which are displayed in the form.

    Perhaps you could look at the code for ‘acf_form’ in ‘core/api.php’ and write something similar with specific fields?

    Thanks
    E

  • Hi Elliot,

    Thanks for this code, its working good for input fields but how can I get values from checkboxes, now it just displays – Array

    I modified the code a bit, to check if value is an array, but it is giving me a Field Name (ugly) not the Field Label

    <?php 
    
    $group_ID = 327;
    
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
    if( $fields )
    {
        foreach( $fields as $field )
        {
            $value = get_field( $field['name'], 'user_' . $author->ID );
            
            echo '<dl>';
                echo '</br><dt>' . $field['label'] . '</dt>';
    
            if(is_array($value))   {
                foreach($value as $singleValue){
                   echo '<dd>' .$singleValue . '</dd> ';
    
                }
    
            } else {
                echo '<dd>' .$value . '</dd> ';
            }
    
                
    
            echo '</dl>';
        }
    }
    
    ?>
  • Seems that this doesn’t work for me. I tried the following:

    $the_query = new WP_Query( array( 'post_type' => 'acf-field-group') );
    
    		// The Group Loop
    		if ( $the_query->have_posts() ) :
    		while ( $the_query->have_posts() ) : $the_query->the_post();
    
    		  $group_ID = get_the_ID();
    		  $name = get_the_title();
    
    		  echo '<p>Group ID: '.$group_ID.', Group name: '.$name.'</p>';
    		  #$content = get_the_content();
    		  #quick(unserialize($content));
    
    		  $fields = array();
    		  $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    		  
    		  
    		  if( $fields )
    		  {
    			  foreach( $fields as $field )
    			  {
    				  $value = get_field( $field['name'] );
    				  
    				  echo '<dl>';
    					  echo '<dt>' . $field['label'] . '</dt>';
    					  echo '<dd>' .$value . '</dd>';
    				  echo '</dl>';
    			  }
    		  } 
    		echo '<hr>';
    		endwhile;
    		endif;
    		
    		// Reset Post Data
    		wp_reset_postdata();

    But $fields is allways empty. Any Idea?

  • same here. i try to get all available fields of a fieldgroup assigned to a specific post type:

    $group_NAME = 'Attribute';
    $the_query = new WP_Query( array( 'post_type' => 'acf-field-group', 's' => $group_NAME) );
    $group_ID = $the_query->post->ID; // returns the valid ID !
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

    but the $fields array is empty!

  • okay. found it!

    $group_NAME = 'Attribute';
    $group_query = new WP_Query( array( 'post_type' => 'acf-field-group', 's' => $group_NAME) );
    $group_ID = $group_query->post->ID;
    $field_query = new WP_Query( array( 'post_type' => 'acf-field', 'post_parent' => $group_ID) );
  • Hey Everyone! Glad this conversation is expanding.
    I’ve run into a new problem with this code.

    It seems like I can’t add conditions to this code within the main If statement, to not display if empty.

    Here’s the issue. I have a header tag above this field group that I want to remove if there are no entries. I just can’t seem to get the code right.
    Here’s what I have now:

      $group_ID = 13249;
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    
    if( $fields )
    {
    echo '<div class="col-sm-6">';
    echo '<h3>Rates:</h3>'; //------I want to remove this line if field is empty, but it keeps appearing
        echo '<ul class="list-unstyled">';
      foreach( $fields as $field_name => $field )
      {
        $value = get_field( $field['name'] );
        if ($field['choices']){
          $map = array(
           'yes' => '<i class="icon-ok"></i>'       
           );
          $value = $map[ $value ];
        } else {
        }
        if( $value && $value != 'no') {
          echo '<li>' . $field['label'] . '&nbsp;' . $value . '</li>';
      }
    }
      echo '</ul>';
    echo '</div>';
    }

    So i need that h3 tag to not show up. What i found is that even when empty, the $fields variable sees an array (just echoing $fields shows ‘array’). So there is no way to say if ’empty’.

    Thoughts?

  • Hi, in now im late but maybe this helps someone else…
    With ACF Pro you can try something like:

    $fields = array();
                    $groups =  acf_get_field_groups(array('post_type' => 'kontakte'));
                    
                     foreach($groups as $group){
                         
                          $fields[$group['title']] = $group;
                          $f = acf_get_fields($group['ID']);
                          foreach($f as $i){
                             $fields[$group['title']]['fields'][$i['name']] = $i;
                             $fields[$group['title']]['fields'][$i['name']]['value'] = get_field($i['key']);
                          }
                         
                     }

    Greets

  • Olá, eu fiz desta maneira.. Não sei se é a melhor, mas funcionou pra mim.

    Com while

    <?php
    $args = array('post_type' => 'acf-field', 'post_parent' => 9);
    $posts_total = new WP_Query( $args ); 
    
    while ( $posts_total->have_posts() ) {
    $posts_total->the_post();
    $values = $posts_total->post;
    
    echo '<pre>';
    echo $values->post_title . '<br>';
    echo $values->post_excerpt . '<br>';
    echo $values->post_name . '<br>';
    echo '</pre>';
    
    } wp_reset_postdata();
    ?>
  • I need a drink after reading all that

  • may i know what is the final code that works please. thanks

  • This is what worked for me. Hides fields with no value.

    
        <?php $fields = acf_get_fields('123'); ?>
    
        <?php if( $fields )
        { 
    	foreach( $fields as $field )
    	{
    		$value = get_field( $field['name'] );
    
    		if ($value) {
    		
    			echo '<dl>';
                    echo '<dt>' . $field['label'] . '</dt>';
                    echo '<dd>' . $field['value'] . '</dd>';
                echo '</dl>';
    		}
    	} 
    
        } 
        ?>
    
  • I just updated the site that started this forum to ACF Pro (3.5 years later!).

    I’m confirming that the code that @joshuaiz has here works. Updated code from my Nov 15, 2015 post above looks like this (different field group id):

    
    echo '<h3>Rates</h3>';
    $fields = acf_get_fields('48269');
    
    echo '<ul class="list-unstyled">';
    if( $fields )
    {
      foreach( $fields as $field )
      {
        $value = get_field( $field['name'] );
        if ($field['choices']){
          $map = array(
           'yes' => '<i class="icon-ok"></i>'       
           );
          $value = $map[ $value ];
        } else {
        }
        if( $value && $value != 'no') {
          echo '<li>' . $field['label'] . '&nbsp;' . $value . '</li>';
      }
    }
    }
      echo '</ul>';
    
Viewing 19 posts - 1 through 19 (of 19 total)

The topic ‘Show an entire specific field group in frontend’ is closed to new replies.