Skip Navigation

[Resolved] Retrieve content from repeatable field group within repeatable field group (PHP)

This thread is resolved. Here is a description of the problem and solution.

Problem: We have one repeatable field group (RFG) that contains another nested RFG. We would like to build a nested array in PHP that contains information from the nested RFG structure, but we are not able to access custom field values in the nested RFG using the toolset_get_related_posts API and get_post_meta.

Solution: In this case the inner, nested toolset_get_related_posts API call must be updated to query by the current outer RFG's post ID as opposed to querying by the ID of the post containing the RFGs. In the case of the outer RFG, querying by the ID of the post is appropriate, but in the case of the inner RFG, you should query by the ID of the outer RFG since the post relationship is technically established between the two RFGs.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 2 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#2131397

Hi

We are using custom fields via Toolset Types to display content on a website via Timber. Therefore we need to retrieve the information from the fields via PHP.

In this case we are working with a repeatable field group 'content-blocks', which has a sub-repeatable field group named 'kolommen'. Unfortunately we are unable to retrieve the content stored inside 'kolommen'. This is our current code:

// RFG - Content Blocks

$child_ids = toolset_get_related_posts(
   $post->ID,
   'content-blocks', //group slug
   'parent',
   100,
   0,
   array('meta_key'=>'toolset-post-sortorder'),
   'post_id',
   'child',
   'meta_value_num',
   'ASC'
);

$items = array();
$a = 0;

foreach( $child_ids as $child ) {
	$items[$a]['titel'] = get_post_meta( $child, 'wpcf-cb-titel', true );
	$items[$a]['afbeelding'] = get_post_meta( $child, 'wpcf-cb-afbeelding', true );	

	$related = toolset_get_related_posts(
	   $post->ID,
	   'kolommen', //group slug
	   'parent',
	   100,
	   0,
	   array('meta_key'=>'toolset-post-sortorder'),
	   'post_id',
	   'child',
	   'meta_value_num',
	   'ASC'
	);

	$kolommen_item = array();
	$b = 0;

	foreach($related as $related_item){
		$kolommen_item[$a][$b]['titel'] = get_post_meta( $related_item, 'wpcf-kolom-subtitel', true );
		$kolommen_item[$a][$b]['inhoud'] = get_post_meta( $related_item, 'wpcf-kolom-inhoud', true );
		$kolommen_item[$a][$b]['kleur'] = get_post_meta( $related_item, 'wpcf-kolom-kleur', true );

		$b++;
	}

	$a++;

}

$context['contentblocks'] = $items;

What do we need to change to the code in order to make this work?

Thanks in advance!

#2131631

Hello there, if the first part of the code retrieves the content-blocks RFGs successfully, we can assume this code is okay:

$child_ids = toolset_get_related_posts(
   $post->ID,
   'content-blocks', //group slug
   'parent',
   100,
   0,
   array('meta_key'=>'toolset-post-sortorder'),
   'post_id',
   'child',
   'meta_value_num',
   'ASC'
);
 
$items = array();
$a = 0;

A nested RFG like kolommen is actually related to its parent content-block RFG, not to the original post. Therefore, you should replace $post->ID in the nested toolset_get_related_posts call with the ID of the current content-block RFG, i.e. $child. In other words, you should adjust this code:

    $related = toolset_get_related_posts(
       $post->ID,
       'kolommen', //group slug
       'parent',
       100,
       0,
       array('meta_key'=>'toolset-post-sortorder'),
       'post_id',
       'child',
       'meta_value_num',
       'ASC'
    );

...use $child instead of $post->ID in the toolset_get_related_posts API call:

    $related = toolset_get_related_posts(
       $child, // the current content-block RFG row
       'kolommen', //group slug
       'parent',
       100,
       0,
       array('meta_key'=>'toolset-post-sortorder'),
       'post_id',
       'child',
       'meta_value_num',
       'ASC'
    );

The following loop should then function as expected to get custom field values from each row of the nested RFG. At first glance, I do not think you need any changes here:

foreach($related as $related_item){
        $kolommen_item[$a][$b]['titel'] = get_post_meta( $related_item, 'wpcf-kolom-subtitel', true );
        $kolommen_item[$a][$b]['inhoud'] = get_post_meta( $related_item, 'wpcf-kolom-inhoud', true );
        $kolommen_item[$a][$b]['kleur'] = get_post_meta( $related_item, 'wpcf-kolom-kleur', true );
 
        $b++;
    }

Try replacing $post->ID with $child in the nested toolset_get_related_posts API call, and if it's not working as expected we can turn on an error log and start logging debug information next if necessary.

#2132497

Hi Christian,

We changed $post->ID with $child but that doesn't seems to completely resolve the issue.

I've made an account for you so you can check for yourself. Can I DM you the credentials?

Thanks in advance!

#2132617

Yes, please provide login credentials in the private reply fields available for your next reply. Please also let me know:
- Where can I find this code - is it in functions.php, or in a custom code snippet in Toolset > Settings > Custom Code, or elsewhere?
- Where can I find the output of this code - is there a test page set up somewhere, or some other way to test the results?

#2134821

We changed $post->ID with $child but that doesn't seems to completely resolve the issue.
Hi, I added a var_dump($kolommen_item) to the PHP, and I see the inner RFG information is added to the kolommen_item array successfully:

var_dump($related);
// The code above outputs the ID's of each inner RFG. 
// The code I added below dumps the information from the kolommen_item array:
var_dump($kolommen_item);

I think you are using the post relationships API correctly. There is a problem elsewhere in your code if the required information is not appearing correctly in the templates. Maybe the $kolommen_item array should be added to the $items array somehow, since it seems you pass $items into the template?

HTML of the template: wp-content/themes/timstrap-master/templates/template-subpage-2.twig
I'm not able to see any templates directory from the wp-admin theme editor.

#2135439

Thanks a lot!

This was indeed the key mistake:
"Maybe the $kolommen_item array should be added to the $items array somehow, since it seems you pass $items into the template?"

All works as intended now.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.