• Hello,

    I’m wondering if it is possible to get all custom fields from a certain group in an array maybe.

    I created a group in types and under that group I created about 50 custom fields. Now to save me some work I was hoping there was a way to get all those custom fields in an array so I can use that to create something like this:

       <td>Field value</td>
        <td>Another fieldname</td>
        <td>And another field value</td>

    Perhaps I’m looking in the wrong places or searching for the wrong things. I’m hoping some one can give me a kick in the right direction.



Viewing 6 replies - 1 through 6 (of 6 total)
  • So in short, I want to use http://codex.wordpress.org/Function_Reference/get_post_custom but only on the fields in a certain group.

    $group_fields = get_post_meta($group_id, '_wp_types_group_fields', true);

    Seems to do the trick! Now I have to find out how to dynamicly find the group_id, but I’m a few steps further already.

    Ok I’ve only gotten so far.

    Somehow it seems I can find the field names, but not the values that go with them.

    Sooo, anyone? 😀

    I came up using this:

    $group_fields = get_post_custom(4);
    $fields = explode(',', $group_fields['_wp_types_group_fields'][0]);
    foreach($fields as $field) {
        if(trim($field) != '') {
            $value = get_post_custom_values('wpcf-'.$field, get_the_ID());
            echo $value[0];

    Anyone have a better solution? Otherwise I’m sticking with this.

    Ok, still not entirely what I need. Now I get the field slug, but I also want the field label.

    So I’m kinda back to square one. I now have a way to find the slug from the fields, but is there no easier way to find the label and the value of the custom fields in a group?

    Since WordPress itself has no native semantics for groups there probably is no hope of doing this only with WordPress functions. Groups are entirely implemented by the Types plugin so it is necessary to understand how it has overload some WordPress functionality to get group semantics. Types saves the information for groups in the SQL table wp_posts using rows of post_type = ‘wp-types-group’ and the information for fields in a group in the SQL table wp_post_meta using rows of meta_key = ‘_wp_types_group_fields’. If you do some SQL queries on these rows you can easily see how Types is storing the group of fields. After doing this I came up this:

    function get_fields_of_group( $group_name, $post_id = NULL ) {
      global $wpdb;
      if ( $post_id === NULL ) { $post_id = get_the_ID(); }
      $sql = 'SELECT meta_value FROM ' . $wpdb->postmeta
        . ' WHERE meta_key = "_wp_types_group_fields" AND post_id = '
        . '(SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = "wp-types-group" '
        . 'AND post_title = "' . $group_name . '")';
      $results = $wpdb->get_results( $sql, ARRAY_A );
      $results = explode( ',', trim( $results[0]['meta_value'], ',' ) );
      $results = ' "wpcf-' . implode( '", "wpcf-', $results ) . '" ';
      $sql = 'SELECT post_id, meta_key, meta_value FROM ' . $wpdb->postmeta
        . ' WHERE post_id = ' . $post_id . ' AND meta_key IN (' . $results
        . ')';
      return $wpdb->get_results( $sql, ARRAY_A );
    # below is for testing
    function prepend_to_content( $content ) {
      # 'Carburetor Fields' is a Group name.
      $content = '<pre>$results = '
        . print_r ( get_fields_of_group( 'Carburetor Fields' ), TRUE)
        . '</pre>' . $content;
      return $content;
    add_filter( 'the_content', 'prepend_to_content' );

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Select all fields in a group’ is closed to new replies.