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.
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
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.
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.
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 />";
}