Types

Displaying Types custom Field values

The easiest way to display custom fields, with just PHP, when you’re using Types plugin, is using Types fields API. A single API function called types_render_field will display all field types, using their correct output formatting. Warning: This must be used inside the loop.

$my_custom_field_value = types_render_field("my-custom-field-id", array("output"=>"raw"));
echo $my_custom_field_value;

If you want to echo custom fields outside the loop then use this:

$frontpage_id = get_option('page_on_front');
$my_custom_field_value = types_render_field("my-custom-field-id", array("post_id"=>"$frontpage_id", "output" => "raw"))

Dealing with Multilines and WYSIWYG

Use html as output, to preserve break lines.

$my_custom_field_value = types_render_field("my-custom-field-id", array("output"=>"html"));
echo $my_custom_field_value;

Displaying Types custom field titles

function get_field_title($key) {
    $wpcf_fields = get_option("wpcf-fields");
    $key = str_replace("wpcf-", "", $key);
    return stripcslashes($wpcf_fields[$key]['name']);
}
// echo get_field_title('wpcf-my-custom-field-slug');

Get all available custom fields that belong to a group

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

Displaying Types repeating fields

$images = get_post_meta($post->ID, 'wpcf-image', false);
foreach ($images as $img) {
    echo $img;
}

Dealing with custom post types

To create the template for a custom post type named ‘client’, make a file single-client.php and edit it. To create the template for it’s archive make a file archive-client.php and edit it.

Say that you have create a new custom post type, with the slug book. To get all the books use:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'book'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
    the_title();
endwhile;
wp_reset_query();

Dealing with custom taxonomies

To get all objects of a specific custom taxonomy use:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'book',
    'tax_query' => array(
            array(
                'taxonomy' => 'book_taxonomy',
                'field' => 'term_id',
                'terms' => '1',
            ),
        ),
);

Find all items that belong to a custom post type

 $args = array(
     'posts_per_page' => -1,
     'meta_query' => array(array('key' => '_wpcf_belongs_book_id', 'value' => get_the_ID()))
);

Find the parent

$parent_id = wpcf_pr_post_get_belongs($post_id, 'custom-type');
$parent_name = get_the_title($parent_id);

Find the parent custom field

$parent_id = wpcf_pr_post_get_belongs($post_id, 'custom-type');
$parent_code = get_post_meta($parent_id, 'wpcf-parent-code', TRUE);

Post A Comment

Anti-Spam Quiz: