Skip Navigation

[Resolved] Display repeatable groups of fields using PHP (Part 2)

This support ticket is created 6 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by chase 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1180393

I started this thread here: https://toolset.com/forums/topic/display-repeatable-groups-of-fields-using-php/#post-1180392

That got marked as resolved so I'm opening it back up.

Here's the last response I've added on that thread and I'll include to start this thread here:

-------------

Christian — I'm trying to get some of this code to work... Here are the errors I'm getting.

I use this code here:

$rfgs = toolset_get_related_posts( 
  $bag_id, 
  'product-slider-item', 
  array( 
    'query_by_role' => 'parent', 
    'return' => 'post_id'
  ),
);

And get this error:

PHP Parse error:  syntax error, unexpected ')' in /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/themes/bagworks02/parts/template-productcarousel.php on line 20

On line 20 is the closing ')' at the end of the code block above.

I'm hoping you can help me get this first part working and then I'll be able to echo out the display of the images and text myself.

#1180618

Hello,

There is an error in your PHP codes, please try to remove the last comma, and test again, for example:

$rfgs = toolset_get_related_posts( 
  $bag_id, 
  'product-slider-item', 
  array( 
    'query_by_role' => 'parent', 
    'return' => 'post_id'
  )
);

Here is a document about toolset_get_related_posts() function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

You can get some example codes by clicking link "Usage examples".

For your reference

#1180965

Thanks, Luo! Feel stupid for missing that.

However, I'm now getting a new PHP error:

Undefined variable: bag_id

As well as a few others:

PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'All provided arguments for a related element must be either an ID or a WP_Post object.' in /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php:223

Stack trace:
#0 /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php(158): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(NULL, 'parent')
#1 /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(103): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(NULL, 'product-slider-...', Array)
#2 /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/themes/bagworks02/parts/template-productcarousel.php(17): toolset_get_related_posts(NULL, 'product-slider-...', Array)
#3 /Users/chasereeves/Drop in /Users/chasereeves/Dropbox/Clients/htdocs/bagworks/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 223

I've spent some time playing with the examples on the page you linked to and my confusion is around how to get this working on our site.

Again, i've got a repeating custom field group (SCREENSHOT: hidden link) which I'm trying to display the data from...

My goal is to display this both on the single "bag" page as well as (hopefully) in a loop.

I really appreciate your eyes on this. Please help me get this sucker setup! Cheers.

#1181095

The message:

Undefined variable: bag_id
</coode>

Means you did not define the var $bag_id in your PHP codes, so it will produce unexpected result.

For example, if current post is a single "slider-item" post, you can get the "slider-item" post ID with WordPress function get_the_ID(), like this:

[php]
$rfgs = toolset_get_related_posts( 
  get_the_ID(), 
  'product-slider-item', 
  array( 
    'query_by_role' => 'parent', 
    'return' => 'post_id'
  )
);

More help:
https://developer.wordpress.org/reference/functions/get_the_ID/
Retrieve the ID of the current item in the WordPress Loop.

#1182449

Thanks, Luo! I got it working.

Just in case someone finds this thread later on, here's the code I'm using:

TO CALL UP THE REPEATABLE FIELD GROUP:

 
$rfgs = toolset_get_related_posts( 
  get_the_ID(),
  'product-slider-item', 
  array( 
    'query_by_role' => 'parent', 
    'return' => 'post_id'
  )
);

TO DISPLAY THE RFG:

foreach( $rfgs as $rfg ) {
  echo types_render_field( "product-slider-image", array( "width" => "300", "height" => "200", "proportional" => "true", "id" => $rfg ) );
  echo "<br />";
  echo types_render_field( "product-slider-image-caption", array( "output" => "normal", "id" => $rfg ) );
  echo "<br />";
}