I'm trying to display content from a repeatable field group, similar to https://toolset.com/forums/topic/display-repeatable-groups-of-fields-using-php/.
I've got the first part working - toolset_get_related_posts is grabbing the correct IDs for the repeatable field groups:
$location_features = toolset_get_related_posts(
$post->ID,
'unique-features',
array(
'query_by_role' => 'parent',
'return' => 'post_id',
'role_to_return' => 'child',
)
);
But when I get to the second step, this doesn't seem to be working:
foreach($location_features as $location_feature) {
echo $location_feature;
echo types_render_field( "wpcf-feature-name", array("output" => "normal", "id" => $location_feature ));
}
The ID gets echoed by the first line within the loop (so it seems like the loop itself is fine), but the second doesn't do anything. wpcf-feature-name is the meta-key for that field according to what I see in the database.
What do I need to do differently here?
Hi Matthew,
Thank you for contacting us and I'd be happy to assist.
The "types_render_field" function expects the target post's ID through the attribute "item":
https://toolset.com/documentation/customizing-sites-using-php/functions/
If the "$location_feature" holds the target post's ID correctly, you can replace:
echo types_render_field( "wpcf-feature-name", array("output" => "normal", "id" => $location_feature ));
With:
echo types_render_field( "wpcf-feature-name", array("output" => "normal", "item" => $location_feature ));
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar! Thanks for the response. I tried using the updated code you sent, but I'm still not seeing the field content echoed - attaching screenshots of what I see in the database for one of the posts in question and what's getting printed on the page. What else should I try?
Hi Matthew,
From the screenshots, the data seems to be stored correctly and should be shown through that code.
Can you please share a clone/snapshot of your website, along with the information about where this code can be seen?
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )
This will allow me to troubleshoot this in more detail, on my own server.
Note: Your next reply will be private.
regards,
Waqar
Thank you for sharing these details and the duplicator package.
I'm downloading it now and will update you with the findings, as soon as this troubleshooting completes.
Hi Matthew,
Thank you for waiting.
During troubleshooting, I noticed that the "types_render_field" function is using the custom field slug with the prefix "wpcf-", which is only needed when using WordPress' native functions ( e.g. get_post_meta ).
With the Toolset's own "types_render_field" function, only field slug (without that prefix) should be used.
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )
types_render_field( "feature-name", array("output" => "normal", "item" => $location_feature ));
This should do the trick and please accept my sincere apologies, as I really should've spotted that in the first reply.
regards,
Waqar
Hooray, that did the trick. Thanks again!