I’ve ended up with a simple solution. after the query to the DB to get all the desired CPT I’m looping the posts object and inside my loop I get the postmeta I will need to add and append them to each post before return the data to the front-end
public function get_contents( WP_REST_Request $request )
{
if( !wp_verify_nonce( sanitize_text_field( $request->get_header( 'X-WP-Nonce' ) ), 'wp_rest' ) ){
return new WP_Error(
'expired_or_invalid_nonce',
__( 'Nonce expired or invalid.' ),
array(
'status' => 403
)
);
}
$post_type = sanitize_text_field( $request->get_param( 'post_type' ) );
$q = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1
)
);
$posts = array();
//
foreach( $q->posts as $p ){
$post_meta = get_post_meta( $p->ID );
if( $post_meta ){
$p->post_meta = $post_meta;
}
array_push( $posts, $p );
}
//
return new WP_REST_Response( $posts, 200 );
}