Skip Navigation

[Résolu] Repeating Fields Groups – Accesible by API / Functions or just Views GUI

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

I am moving a site over from using CMB2 to take advantage of the upcoming relationships API.

It contains repeating fields but using CMB2, they are just stored as a serialized array in post meta.

From reading the documentation, it seems Toolset stores them within a hidden CPT, and all I can see is tutorials for accessing them by using a View/GUI.

I wasn't intending to use Views for this project. Do you have documentation for how to access them with API Functions?

Solution:

There isn't exact document for the Repeating Fields Groups API, but it is using child post type to store the Repeating Fields Groups , so you can use function toolset_get_related_posts() to get the child posts, then display custom field of each child post, for example:

1) Create a Repeating Fields Groups "my-fields-group" in post type "my-cpt", add some custom field into the field group

2) In the your theme file single.php, add below codes:

$child_posts = toolset_get_related_posts( 
    get_the_ID(), //Post to query by.
    'my-fields-group',  //Slug of the relationship to query by 
    'parent', //Name of the element role to query by.
    100, //Maximum number of returned results
    0, //Result offset
    array(),//Additional query arguments
    'post_id', //Determines return type
    'child' // which posts from the relationship should be returned
);
var_dump($child_posts);

As you can see the last parameter is "child", it will be able to query the child posts of current post.

Relevant Documentation:

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

This support ticket is created Il y a 6 années et 2 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 réponses, has 2 voix.

Last updated by Luo Yang Il y a 6 années et 1 mois.

Assisted by: Luo Yang.

Auteur
Publications
#623683

Hi there,

I am moving a site over from using CMB2 to take advantage of the upcoming relationships API.

It contains repeating fields but using CMB2, they are just stored as a serialized array in post meta.

From reading the documentation, it seems Toolset stores them within a hidden CPT, and all I can see is tutorials for accessing them by using a View/GUI.

I wasn't intending to use Views for this project. Do you have documentation for how to access them with API Functions?

Thanks again

Jeff

#624122

Dear Jeff,

There isn't exact document for the Repeating Fields Groups API, but it is using child post type to store the Repeating Fields Groups , so you can use function toolset_get_related_posts() to get the child posts, then display custom field of each child post, for example:
1) Create a Repeating Fields Groups "my-fields-group" in post type "my-cpt", add some custom field into the field group
2) In the your theme file single.php, add below codes:

$child_posts = toolset_get_related_posts( 
	get_the_ID(), //Post to query by.
	'my-fields-group',  //Slug of the relationship to query by 
	'parent', //Name of the element role to query by.
	100, //Maximum number of returned results
	0, //Result offset
	array(),//Additional query arguments
	'post_id', //Determines return type
	'child' // which posts from the relationship should be returned
);
var_dump($child_posts);

As you can see the last parameter is "child", it will be able to query the child posts of current post.

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

#624225

Hey Luo,

That's great thanks.

Jeff

#624466

You are welcome

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