Support

Account

Home Forums ACF PRO List all ACF fields

Solving

List all ACF fields

  • Hi,

    for some reason, I need a way to display all ACF-fields in an admin page. Ideally, I would like to display them related to their post types, like so:

    • My post type 1
      • Name: field_xxxx, label: My acf field 1
      • Name: field_xxxx, label: My acf field 2
    • My post type 2
      • Name: field_xxxx, label: My acf field 1
      • Name: field_xxxx, label: My acf field 2

    But it would be OK if I just could display them anyway. I need the field names and the field labels as an array.

    Does anybody have an idea how to get this result?

  • Hi @sixtyseven

    There isn’t a way I know of to display all fields other than running a query on the wp_postmeta table yourself and discerning what’s an ACF field and what isn’t by testing or joining to the wp_posts table.

    But, if you have a particular ID of a page or post or options page that you want to get the custom fields for, you can use get_field_objects($postID); to get an array of fields along with various details about each field.

    You can then loop through them and use get_field('field_name', $postID); to get the value of a particular field if you need to show values too.

    I recently needed to do something similar to generate a ‘report’ which listed all custom fields and exported to a .csv – to do this, I first created a draft post with all custom fields filled in, saved it & used get_field_objects() for this post. I then took the resulting array & looped through it for each of the other posts to get the data. Perhaps not the most efficient way but it works.

    Hope this helps

    cheers
    alan

  • I tried another approach by looping through the acf groups first, but without any luck. Check this one:

    $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>';
    
    		  $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>';
    			  }
    		  } 
    
    		endwhile;
    		endif;
    		
    		// Reset Post Data
    		wp_reset_postdata();

    No matter what, $fields is always empty 🙁

  • I have tried many things but none works except of this 🙂

        global $wpdb;
    
        $qry =  "SELECT post_excerpt as 'field_name', post_name as 'field_key' FROM <code>wp_posts</code> where post_type = 'acf-field'";
    
        $results = $wpdb->get_results( $qry, ARRAY_A );

    Wrap it in a function in the functions.php file.

    Hope this works

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘List all ACF fields’ is closed to new replies.