Skip Navigation

[Resolved] Writing RFG to CPT via REST

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can we add Repeatable Field Groups to a Post using the REST API and Toolset?

Solution:
You can set repeating field groups the same way as you'd set a post belonging to another post.
https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/

Since RFG (Repeatable Field Groups) are in fact Posts, gathering a set of fields, you'd need to threat the RFG itself as a Post (and that RFG will belong to the Post where it's added to).
The Fields within the RFG are to see as custom fields like when added to a post type, just here the post type is the RFG.

However, to "get" RFG's, to have this incorporated in Toolset you'd have to submit a Feature Request for it in https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For now, you'd be able to add your own custom endpoint, if you are versed with the WordPress REST API which provides API endpoints for WordPress data types, you could do that, but we can not assist it here in the Support.

An example of adding such an endpoint, using a post-relationship as an example with "customendpoint" namespace can be seen here https://toolset.com/forums/topic/writing-rfg-to-cpt-via-rest/#post-1310253

This support ticket is created 5 years, 4 months 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by steveL-12 5 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1308313

Tell us what you are trying to do?
One of my custom post types uses a repeating field group (RFG). Even though I have enabled REST for the CPT and do indeed see the other CFs when I query via REST, I see nothing of the RFGs associated with a post. Nor do i know how to specify the RFG information for a new post. The documentation doesn't seem to mention RFGs at all from what I've seen.

I am trying to move my data from my old database on an old proprietary site to Toolset.

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/
and
https://developer.wordpress.org/rest-api/

But there seems to be no information in there on how to get at (or write) the RFG data for a post.

Is there a similar example that we can see?
I don't know - I'd love to see an example of this myself rather.

In a nutshell, I have a CPT called Issue wherein each post is an issue of an academic journal, along with various CFs. The CPT also has an RFG with entries which form a table of contents. So the fields in each RFG entry would be things like page number and chapter title. I need to load the data somehow but there seems to be no built-in mechanism for loading the data and no documented REST api method. Do you know of a way to load the data (I can generate any kind of export from the old system easily).

What is the link to your site?
hidden link

#1308559

You can set repeating field groups the same way as you'd set a post belonging to another post.
Since RFG (Repeatable Field Groups) are in fact Posts, gathering a set of fields, you'd need to threat the RFG itself as a Post (and that RFG will belong to the Post where it's added to).
The Fields within the RFG are to see as custom fields like when added to a post type, just here the post type is the RFG.

However the REST API does not allow to get related posts, so I don't think it's possible to get RFG's of a given post either.
It'd be possible to update them (with the fake "post" field as mentioned here https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/#field-types-and-their-value-format-in-rest-api), but not to get them.

I'll ask our Developers for confirmation about this and let you know here.

#1308613

Escalating until I've got an answer.

#1310253

To have this incorporated in Toolset you'd have to submit a Feature Request for it in https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For now, you'd be able to add your own custom endpoint, if you are versed with the WordPress REST API which provides API endpoints for WordPress data types, you could do that, but we can not assist it here in the Support.

An example of adding such an endpoint, using a post-relationship as an example with "customendpoint" namespace:

register_rest_route( 'customendpoint/v1', '/related/(?P<id>\d+)', array(
        'methods' => 'GET',
        'callback' => 'rest_related_posts'
    ) );

See https://developer.wordpress.org/reference/functions/register_rest_route/

Then, the method example rest_related_posts, we get related posts with toolset_get_relationship API:

function rest_related_posts( $request ){

        $parameters = $request->get_query_params();

        if ( isset( $parameters['relationship'] ) ) {

            $relationship = toolset_get_relationship( $parameters['relationship'] );

            if ( $relationship ) {

                $parent = $relationship['roles']['parent']['types'][0];
                $child = $relationship['roles']['child']['types'][0];

                $post = get_post( $request['id'] );
                $type = $post->post_type;

                $origin = ( $parent == $type ) ? 'parent' : 'child';

                // Get connected posts
                $connections = toolset_get_related_posts( $post->ID, $parameters['relationship'], array(
                    'query_by_role' => $origin,
                    'role_to_return' => 'other',
                    'return' => 'post_id'
                    )
                );

                return implode(',',$connections);
            }
        }
    }

This is subject to custom code, which we can not assist fully in the Support Forum here.
If you need help with this, we can recommend these contractors https://toolset.com/contractors/

#1313765

My issue is resolved now. Thank you!