• Magicways

    (@akash-khurana)


    Why register_rest_field not working for cusrom end points ? how to make it work for custom end ponts of custom post types.

    add_action( 'rest_api_init', function () {
        register_rest_route( 'reviews/v1', '/(?P<id>\d+)', array(
            'methods' => 'GET',
            'callback' => 'get_course_reviews',
    		'show_in_rest' => true, 
    		 'rest_controller_class' => 'WP_REST_Posts_Controller'
        ) );
    } );
    register_rest_field( 
    	'reviews/v1',
    	'is_sset_img',
        array(
            'get_callback'    => 'is_sset_img',
            'update_callback' => null,
            'schema'          => null,
             )
       );
    }

    • This topic was modified 2 years, 4 months ago by Yui. Reason: moved to "developing with wordpress"
Viewing 2 replies - 1 through 2 (of 2 total)
  • From the docs for register_rest_field() you’re giving the wrong value as the first argument.

    The first argument is meant to be the object type (post, page, term, etc), not the REST route. If you change that out to be the correct object type it should work.

    Thread Starter Magicways

    (@akash-khurana)

    I have custom post type wdm_course_review .
    I already tried following but still dont work. Please help this is driving me crazy & no help getting from internet.
    fowwlowing only works for the default route /wp-json/wp/v2/wdm_course_review not the custom route /reviews/v1/<course_id>

    
    register_rest_field( 
    	'wdm_course_review',
    	'is_sset_img', //
        array(
            'get_callback'    => 'is_sset_img',
            'update_callback' => null,
            'schema'          => null,
    		'show_in_rest' => true, 
             )
       );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Register Rest field don’t work’ is closed to new replies.