Skip Navigation

[Resolved] Can’t display repeatable group Fields in a php function

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

Problem:
Can't display repeatable group Fields in a php function

Solution:
You can use the Toolset post relationship API function toolset_get_related_posts() to display related posts using PHP.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/cant-display-repeatable-group-fields-in-a-php-function/#post-1340939

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

This support ticket is created 5 years, 3 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Daniel Stadeli 5 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1340665
repeatable fields result - empty.jpg
repeatable fields shortcode.jpg
show repeatable fields function.jpg
repeatable group bibelstellen.jpg

I am trying to: display fields in a repeatable group

Link to a page where the issue can be seen: hidden link

I expected to see: field values = Johannes 5,1-10; Matthäus 4,5; 1. Mose 5,1-8;

Instead, I got: nothing (see screenshot)

I have set up custom fields for the posts post type, one of them is a repeatable group called 'bibelstellen' and it contains a select field 'buch', and 3x number fields: kapitel, vers-von, vers-bis.
I can show these fields using a toolset view, but there are ristrictions my client cannot live with: I get extra <p> characters which I can't remove (tried for 3 hours!!) and I cannot use conditional output. (vers-bis can be emtpy sometimes and I need to hide the dash in such a case).

I NEED THIS TO WORK IN A PHP FUNCTION.

code in functions.php (I spent 8 hours looking through all the examples, they are incomplete or don't apply to repeatable groups):

//show Bibelstellen-repeating field
function bibelstellen_direkt_func()
{
$child_posts = toolset_get_related_posts(get_the_ID(),'bibelstellen', array('query_by_role' => 'parent', 'return' => 'post_object'));
if ($child_posts):
foreach ($child_posts as $bst) :
$bibref = (types_render_field( 'buch', array()));
$bibref .= (types_render_field( 'kapitel', array()));
$bibref .= (types_render_field( 'vers-von', array()));
endforeach;

return ($bibref);
endif;
return ('none');
}
add_shortcode( 'bibelstellen-direkt', 'bibelstellen_direkt_func' );
//I use the shortcode in my content template for regular wordpress posts

Can you please help me to get this function to work?

#1340939

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - I've added the following code to the "Custom Code" section offered by Toolset:
=> hidden link

//Bibelstellen-repeating field direkt anzeigen
function bibelstellen_direkt_func1() {
  
 global $post;
  
  $child_posts = toolset_get_related_posts(
                                    $post->ID,
                                    'bibelstellen',
                                     'parent',
                                     100,
                                      0,
                                      array(),
                                     'post_object',
                                     'child'
                                     );
 if ($child_posts):
     $bibref = '';
	  foreach ($child_posts as $bst) :
		 $bibref .= (types_render_field( 'buch', array('item'=>$bst->ID)))." <br />";
		 $bibref .= (types_render_field('kapitel', array('item'=>$bst->ID)))." <br />";
		 $bibref .= (types_render_field( 'vers-von', array('item'=>$bst->ID)))." <br />";
	  endforeach;
	
	  return ($bibref);
	endif;
	return ('none');
}
add_shortcode( 'bibelstellen-direkt1', 'bibelstellen_direkt_func1' );

I can see now that its displaying the Repeating Field Group entries on the following page:
=> hidden link

You can remove the code you added to your functions.php file as its not used.

#1341169

My issue is resolved now. Thank you very much!