0

I have an endpoint in this address:

example.com/wp-json/wp/v2/myendpoint

I want to add a field to the endpoint with these codes:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
 
function create_api_posts_meta_field() {    
    register_rest_field( 
        'myendpoint',
        'post-meta-fields',
        array(
           'get_callback'    => 'get_post_meta_for_api',
        )
    );
}
 
function get_post_meta_for_api( ) {
    $hello="Hello";
    return $hello;
}

but it doesn't work. Please tell me whats wrong with my codes.

3
  • Your code seems fine, so how exactly is it not working - did you see any error, or your custom REST field doesn't appear in the response? Are you sure the post type slug is correct - which in the question is myendpoint?
    – Sally CJ
    Oct 16, 2020 at 14:45
  • Hi, myendpoint is existed and working correctly. I want to add "post-meta-fields" to it. but it doesn't show "post-meta-fields" in this address: example.com/wp-json/wp/v2/myendpoint
    – Sadegh
    Oct 16, 2020 at 14:55
  • Can you edit your question and include the post type registration code?
    – Sally CJ
    Oct 16, 2020 at 15:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.