skip to Main Content

I’m trying to add a custom field to the REST API response for my custom post type in WordPress. The field is supposed to show the value of my_custom_field. However, the custom field is not appearing in the response.

Here’s the code I’m using:

function add_custom_fields_to_rest_api() {
    register_rest_field(
        'my_custom_post_type',
        'my_custom_field',
        array(
            'get_callback' => 'get_custom_field_value',
            'update_callback' => null,
            'schema' => null,
        )
    );
}

function get_custom_field_value($object, $field_name, $request) {
    return get_post_meta($object->ID, $field_name, true);
}

add_action('rest_api_init', 'add_custom_fields_to_rest_api');

What am I doing wrong?

2

Answers


  1. The issue lies in how you’re accessing the post ID in the get_custom_field_value function. The $object parameter should be treated as an array, not an object.

    Here is how your code should look like:

    function add_custom_fields_to_rest_api() {
        register_rest_field(
            'my_custom_post_type',
            'my_custom_field',
            array(
                'get_callback' => 'get_custom_field_value',
                'update_callback' => null,
                'schema' => null,
            )
        );
    }
    
    function get_custom_field_value($object, $field_name, $request) {
        return get_post_meta($object['id'], $field_name, true);
    }
    
    add_action('rest_api_init', 'add_custom_fields_to_rest_api');
    

    In the get_custom_field_value function, $object['id'] correctly accesses the post ID. This should ensure that my_custom_field appears in the REST API response as expected.

    Login or Signup to reply.
  2. add_action('rest_api_init', 'restapi_fetch_data');
    function restapi_fetch_data()
        {
            register_rest_route( 'testapidata', '/getalldata', array(
                                array(
                                        'methods'             => 'GET',
                                        'callback'            => 'get_items_all_data',
                                        'args'                => array(),
                                )
                                ) 
            );
            register_rest_route( 'testapidata', '/getproductdata', array(
                                array(
                                        'methods'             => 'GET',
                                        'callback'            => 'getproduct_all_data',
                                        'args'                => array(),
                                )
                                ) 
            );
        }
    function get_items_all_data()
        {
            $result=array();
            $result['name'] ='hello';
            return $result;
        }
    function getproduct_all_data()
        {
            $product_all_result = array();
            $pro_args = array(
                        'status'    => array('publish' ),
                        'type'      => array_merge( array_keys( wc_get_product_types() ) ),
                        'limit'     => -1,
                        'orderby'   => 'ID'
                    );
            $products_all_data = wc_get_products( $pro_args );
            foreach ($products_all_data as $products_all_data_key => $products_all_data_value) {
                $product_all_result[$products_all_data_key]['id'] = $products_all_data_value->get_id();
                $product_all_result[$products_all_data_key]['name'] = $products_all_data_value->get_name();
            }
            
            return $product_all_result;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search