I think the problem here is how you register the rest routes and how you return data for the single project endpoint.
When you register the route and the callback like this.
function single_project($data) {
$post_ID = $data['id'];
return get_post($post_ID);
}
add_action('rest_api_init', function () {
register_rest_route( 'project/v1', 'post/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'single_project',
'args' => ['id']
));
});
You'll just get the default fields the WP_Post
object has. The data does not get processed the same way as regular posts do. This means that register_rest_field()
does not have a chance to add the additional field to the response.
If you did something like
class WP_REST_Projects_Controller extends WP_REST_Posts_Controller {
public function register_routes() {
register_rest_route( 'project/v1', 'post/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => [],
));
}
}
add_action('rest_api_init', function () {
(new WP_REST_Projects_Controller('project'))->register_routes();
});
then the additional field would get added to the response, as the global variable which register_rest_field()
affects is used inside WP_REST_Controller
which WP_REST_Posts_Controller
extends. Inside get_additional_fields()
method to be specific.
You should either extend the controller class and override it as needed or modify the single_project()
callback to return additional fields along the post fields.
register_post_type
did you set'show_in_rest' => true
?$post_id
gets a value? (It's possible that$object['id']
should be$object['ID']
.) 2.get_post_meta()
called without a$key
parameter will return all of a post's metadata; is that your intent?function.php
but not from inside functions likeregister_featured_video()
orget_featured_video()
.error_log()
to print it to your error log, if you've got debugging enabled. (By default the error log would be/wp-content/debug.log
ifWP_DEBUG_LOG
istrue
.)wp-config.php
:define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', true );
but I still don't seedebug.log
file appearing anywhere in my folder tree after reloading my site's page many times. Any idea?