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:
<?php
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' );
?>