Skip Navigation

[Resuelto] Repeating Fields Groups – Accesible by API / Functions or just Views GUI

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 6 años, 8 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 3 respuestas, tiene 2 mensajes.

Última actualización por Luo Yang hace 6 años, 8 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#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