Thanks for the links to the WP codex but they don’t address my question, which was about accessing Types data.
I have found my own solutions which I will share in the hope that it will assist someone else.
Why would I want to retrieve all of the custom fields associated with a custom post type? In short, to control visibility.
My client wanted users to be able to submit information through a front-end form (not the admin dashboard). There are five different custom post types and two dozen custom fields, some of which are common between the post types (like name, email, etc) and some of which are unique to the post type. The idea was to show the common fields and, using a drop-down, select the appropriate custom post type and display those fields.
Here is the function I wrote; it performs several queries on the WP DB, pulling the info stored by the Types plugin. Given a custom post type, the function will return an array of the custom fields plus all meta stored with those fields, including name, slug, description, placeholder, required and validation. It allows me to create a dynamic form that is completely managed by the client from the Types interface.
function get_custom_fields_by_post_type($post_type) {
global $wpdb;
$query = "
SELECT *
FROM $wpdb->postmeta AS pm1, $wpdb->postmeta AS pm2, $wpdb->posts
WHERE pm1.meta_key = '_wp_types_group_post_types'
AND pm1.meta_value LIKE '%$post_type%'
AND pm2.post_id = pm1.post_id
AND pm2.meta_key = '_wp_types_group_fields'
AND $wpdb->posts.ID = pm2.post_id
ORDER BY $wpdb->posts.post_title ASC
";
$results = $wpdb->get_results ( $query );
$cf_meta = get_custom_field_meta();
$i=0;
$my_cfs[post_type] = $post_type;
foreach($results as $result) {
$my_cfs[data][$i][group_name] = $result->post_title;
$my_cfs[data][$i][group_slug] = $result->post_name;
$the_fields = explode(',',$result->meta_value); // custom fields stored as csv string, but with commas at front and back
$the_fields = array_filter($the_fields); // deletes empty array elements
$x=0;
foreach($the_fields as $the_field) {
$my_cfs[data][$i][fields][$x] = $cf_meta[$the_field];
$x++;
}
$i++;
}
return $my_cfs;
}
This function returns the same data but queries on Types Custom Field Group instead of custom post type:
function get_custom_fields_by_group($group_slug) {
global $wpdb;
$query = "
SELECT *
FROM $wpdb->posts,$wpdb->postmeta
WHERE $wpdb->posts.post_name LIKE '%$group_slug%'
AND $wpdb->postmeta.post_id = $wpdb->posts.ID
AND $wpdb->postmeta.meta_key like '_wp_types_group_fields'
";
$results = $wpdb->get_results ( $query );
$my_cfs = (array)$results[0]; // converts object to array
$cf_meta = get_custom_field_meta();
$the_fields = explode(',',$results[0]->meta_value); // custom fields stored as csv string, but with commas at front and back
$the_fields = array_filter($the_fields); // deletes empty array elements
$x=0;
foreach($the_fields as $the_field) {
$my_cfs[fields][$x] = $cf_meta[$the_field];
$x++;
}
return $my_cfs;
}
Both functions rely on this helper function. Types stores all of the custom field relational data in one big serialized array, meaning you can’t query it directly. Instead, we just retrieve the info, unserialize it, and give it to the other functions.
function get_custom_field_meta() {
global $wpdb;
$cf_meta = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = 'wpcf-fields'");
$cf_meta = unserialize($cf_meta->option_value);
return $cf_meta;
}