• I am using the Co-authors plus plugin and I want to write field by JSON to create a new post. Here is the code I found but it only get coauthors key back in response, not write to post it. Please help me!

    if ( function_exists('get_coauthors') ) {
        add_action( 'rest_api_init', 'custom_register_coauthors' );
        function custom_register_coauthors() {
            register_rest_field( 'post',
                'coauthors',
                array(
                    'get_callback'    => 'custom_get_coauthors',
                    'update_callback' => null,
                    'schema'          => null,
                )
            );
        }
    
        function custom_get_coauthors( $object, $field_name, $request ) {
            $coauthors = get_coauthors($object['id']);
    
            $authors = array();
            foreach ($coauthors as $author) {
                $authors[] = array(
                    'display_name' => $author->display_name,
                    'user_nicename' => $author->user_nicename
                );
            };
    
            return $authors;
        }
    }

The topic ‘Write field Co-Authors Plus JSON REST API’ is closed to new replies.