Wordpress: Register Repeater Field with RestAPI

147 views Asked by At

I am trying to register a Custom Post Type within WordPress RestAPI. I have gotten most of it to work (the post itself and the fields), but I am stuck on getting the repeater field to register. Here is what I have so far.

Here is a screenshot of the repeater field:

enter image description here

This bit of code is in my theme folders within the functions.php file. I have taken out a bunch of fields to shorten the code as they are all strings and are working just like the given field "reservation-status".

add_action( 'init', 'booking_register_meta' );
function booking_register_meta(){

    //This bit registers my custom post type
    register_post_type(
        'reservations',
        array(
            'labels' => array(
                'name' => _( 'Reservations' ),
                'singular_name' => _( 'Reservation' ),
                ),
                'public' => true,
                'has_archive' => true,
                'show_in_rest' => true,
                'supports' => array(
                    'title',
                    'custom-fields',
                    'revisions',
                    )
                )
            );

            //this bit registers my regular field
            register_meta(
                'post',
                'reservation-status',
                array(
                    'single'       => true,
                    'type'         => 'string',
                    'default'      => '',
                    'show_in_rest' => true,
                    'object_subtype' => 'reservations'
                )
            );

            //this bit is suppose to register my repeater field
            register_post_meta(
                'post',
                'occupants',
                array(
                    'single'       => false,
                    'type'         => 'array',
                    'show_in_rest' => array(
                        'schema' => array(
                            'items' => array(
                                'type'       => 'string',
                                'properties' => array(
                                    'occupant-first-name'    => array(
                                        'type' => 'string',
                                    ),
                                    'occupant-last-name' => array(
                                        'type'   => 'string',
                                    ),
                                ),
                            ),
                        ),
                    ),
                )
            );
}

I am using the documentation from here: Wordpress: Modifying Responses

Here is my JSON output when I use postman.(The fields with no values are correct). Note: Occupants is missing under meta. This is a GET request and the occupants repeater field for this post has information input.

[
    {
        "id": 88737,
        "date": "2023-10-11T03:47:52",
        "date_gmt": "2023-10-11T07:47:52",
        "guid": {
            "rendered": "https://example.com/reservations/myemailgmail-com/"
        },
        "modified": "2023-10-15T22:43:12",
        "modified_gmt": "2023-10-16T02:43:12",
        "slug": "myemailgmail-com",
        "status": "publish",
        "type": "reservations",
        "link": "https://example.com/reservations/myemailgmail-com/",
        "title": {
            "rendered": "[email protected]"
        },
        "content": {
            "rendered": "",
            "protected": false
        },
        "template": "",
        "meta": {
            "_jf_save_progress": "",
            "reservation-status": "Pending Reservation",
            "reservation-number": "",
            "confirmed-reservation-id": "",
            "check-in": "2023-10-11",
            "check-out": "2023-10-18",
            "resort": "Bay Lake Tower",
            "room": "Deluxe Studio",
            "view": "Standard View",
            "room-request": "",
            "renter-first-name": "John",
            "renter-last-name": "Doe",
            "renter-full-name": "John Doe",
            "email": "[email protected]",
            "phone": "11231231234",
            "address": "Platform 9 3/4",
            "state": "",
            "points": "",
            "price-per-point": "",
            "deposit": "",
            "deposit-status": "",
            "total": "",
            "total-with-fee": "",
            "balance-status": "",
            "balance": "",
            "balance-with-fee": "",
            "balance-due-date": "",
            "date-balance-paid": "",
            "paypal-fee": "",
            "payment-method": "",
            "travel-credit-number": "",
            "promo-code": "",
            "contract-status": "",
            "7-month-window": "",
            "11-month-window": "",
            "comments": ""
        },
        "_links": {
            "self": [
                {
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737"
                }
            ],
            "collection": [
                {
                    "href": "https://example.com/wp-json/wp/v2/reservations"
                }
            ],
            "about": [
                {
                    "href": "https://example.com/wp-json/wp/v2/types/reservations"
                }
            ],
            "version-history": [
                {
                    "count": 1,
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions"
                }
            ],
            "predecessor-version": [
                {
                    "id": 88739,
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions/88739"
                }
            ],
            "wp:attachment": [
                {
                    "href": "https://example.com/wp-json/wp/v2/media?parent=88737"
                }
            ],
            "curies": [
                {
                    "name": "wp",
                    "href": "https://api.w.org/{rel}",
                    "templated": true
                }
            ]
        }
    }
]

UPDATE!!!! Per the first answer I have changed "posts" to "reservations". See code below:

register_post_meta(
    'reservations',
    'occupants',
    array(
        'single'       => false,
        'type'         => 'array',
        'show_in_rest' => array(
            'schema' => array(
                'items' => array(
                    'type'       => 'string',
                    'properties' => array(
                        'occupant-first-name'    => array(
                            'type' => 'string',
                        ),
                        'occupant-last-name' => array(
                            'type'   => 'string',
                        ),
                        
                        'occupant-full-name' => array(
                            'type'   => 'string',
                        ),
                        'occupant-age' => array(
                            'type'   => 'string',
                        ),
                        
                        'occupant-gender' => array(
                            'type'   => 'string',
                        ),
                    ),
                ),
            ),
        ),
    )
    );

Now my JSON response is this (I see Occupants, but it shows as null and there should be data there):

[
    {
        "id": 88737,
        "date": "2023-10-11T03:47:52",
        "date_gmt": "2023-10-11T07:47:52",
        "guid": {
            "rendered": "https://example.com/reservations/myemailgmail-com/"
        },
        "modified": "2023-10-15T22:43:12",
        "modified_gmt": "2023-10-16T02:43:12",
        "slug": "myemailgmail-com",
        "status": "publish",
        "type": "reservations",
        "link": "https://example.com/reservations/myemailgmail-com/",
        "title": {
            "rendered": "[email protected]"
        },
        "content": {
            "rendered": "",
            "protected": false
        },
        "template": "",
        "meta": {
            "_jf_save_progress": "",
            "reservation-status": "Pending Reservation",
            "reservation-number": "",
            "confirmed-reservation-id": "",
            "check-in": "2023-10-11",
            "check-out": "2023-10-18",
            "resort": "Bay Lake Tower",
            "room": "Deluxe Studio",
            "view": "Standard View",
            "room-request": "",
            "renter-first-name": "John",
            "renter-last-name": "Doe",
            "renter-full-name": "John Doe",
            "email": "[email protected]",
            "phone": "11231231234",
            "address": "Platform 9 3/4",
            "state": "",
            "occupants": [
                null
            ],
            "points": "",
            "price-per-point": "",
            "deposit": "",
            "deposit-status": "",
            "total": "",
            "total-with-fee": "",
            "balance-status": "",
            "balance": "",
            "balance-with-fee": "",
            "balance-due-date": "",
            "date-balance-paid": "",
            "paypal-fee": "",
            "payment-method": "",
            "travel-credit-number": "",
            "promo-code": "",
            "contract-status": "",
            "7-month-window": "",
            "11-month-window": "",
            "comments": ""
        },
        "_links": {
            "self": [
                {
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737"
                }
            ],
            "collection": [
                {
                    "href": "https://example.com/wp-json/wp/v2/reservations"
                }
            ],
            "about": [
                {
                    "href": "https://example.com/wp-json/wp/v2/types/reservations"
                }
            ],
            "version-history": [
                {
                    "count": 1,
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions"
                }
            ],
            "predecessor-version": [
                {
                    "id": 88739,
                    "href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions/88739"
                }
            ],
            "wp:attachment": [
                {
                    "href": "https://example.com/wp-json/wp/v2/media?parent=88737"
                }
            ],
            "curies": [
                {
                    "name": "wp",
                    "href": "https://api.w.org/{rel}",
                    "templated": true
                }
            ]
        }
    }
]
1

There are 1 answers

3
Jarmo T On

If you look at the register_post_meta() function, the first parameter should be the post type that the meta field will be registered for. Currently, you are adding the occupants field to the post post type.

From your code it looks like your custom post type is reservations, so you should be registering the occupants field for the reservations post type.

Something like:

register_post_meta(
                'reservations',
                'occupants',
                array(
                    'single'       => false,
                    'type'         => 'array',
                    'show_in_rest' => array(
                        'schema' => array(
                            'items' => array(
                                'type'       => 'string',
                                'properties' => array(
                                    'occupant-first-name'    => array(
                                        'type' => 'string',
                                    ),
                                    'occupant-last-name' => array(
                                        'type'   => 'string',
                                    ),
                                ),
                            ),
                        ),
                    ),
                )
            );

Have a closer look at register_post_meta on the WordPress documentation.

Also, it's good practice to name your custom post types in a singular form.