Select all fields in a group (7 posts)

  1. Mark Jansen
    Posted 2 years ago #


    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.



  2. Mark Jansen
    Posted 2 years ago #

    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.

  3. Mark Jansen
    Posted 2 years ago #

    $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.

  4. Mark Jansen
    Posted 2 years ago #

    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? :D

  5. Mark Jansen
    Posted 2 years ago #

    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.

  6. Mark Jansen
    Posted 2 years ago #

    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?

  7. Magenta Cuda
    Posted 2 years ago #

    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' );

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Types
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic