The Types plugin allows you to create relationships between different post types. This page explains how to use PHP to display repeatable field groups belonging to a parent post. Alternatively, you can use the Toolset suite of plugins to display related contents without programming.

Displaying repeatable field group

Displaying repeatable field groups is no different from displaying related child posts.

To display content from child posts you will need to use the toolset_get_related_posts function. It considers only one attribute: the related child post type from which you wish to display the posts.

This function will create an array containing the contents of all child posts related to the current post. Then, we need to loop through this array and display the desired information about each of the child posts.

The following code will display the title and an image that is displayed using a custom field, for all related “Car” child posts:

Example for display repeatable groups of fields
<?php 
$child_posts = toolset_get_related_posts( get_the_ID(), 'car-rfg-slug', array( 'query_by_role' => 'parent', 'return' => 'post_object' ) );
foreach ($child_posts as $child_post) 
{ ?>
         
<div class="car-listing">
        <h5><?php echo $child_post->post_title; ?></h5>
        <?php echo types_render_field( "car-photo", array( "id"=> "$child_post->ID", "size" => "thumbnail" )); ?>
    </div>
 
 
<?php } ?>

Let us go through this example step by step:

  • First, we load the contents of related “car” child posts into a $child_posts array.
  • Then, we start a foreach loop to iterate through that $child_posts array.
  • Finally, in the loop we display any contents of the “car” posts that we require.
    • We use $child_post->post_title to display the title of the book
    • We use types_render_field to display the photo of a car using a custom image field created with Types.

If you require more control over the returned posts, you may directly create a custom query. The following example returns all cars that belong to the current manufacturer and sorts them based on the “engine type” custom field.

Example of custom query for display of repeatable groups of fields
$child_args = array(
    'post_type' => 'car',
    'numberposts' => -1,
    'meta_key' => 'wpcf-engine-type',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'toolset_relationships' => array(
        'role' => 'child',
        'related_to' => get_the_ID(),
        'relationship' => 'my-relationship-slug'
    )
);
 
$query = new WP_Query( $child_args );
$child_posts = $query->posts;

Please note the toolset_relationships query argument. You can find detailed information about it on our page about Post Relationships API.

Besides using the post_title to display the title, you can display any information belonging to a child post. You can find the list of post properties you can display on the official WordPress documentation page about $post data.