First off, there’s a default endpoint for retrieving pages, but I presumed that you intentionally did not use it?
Now the issues with your code:
-
That
$request->get_params()
gets all the parameters and not a single one. So for single parameters, you should use$request->get_param()
.$slug = $request->get_params('slug'); // incorrect $slug = $request->get_param( 'slug' ); // correct
-
The error
500
is likely because there’s no function namedget_page_meta
in WordPress. Did you mean to useget_post_meta
?$page_meta = get_page_meta( $page->ID ); // incorrect $page_meta = get_post_meta( $page->ID ); // correct // or do you have a custom get_page_meta() function?
-
As I already mentioned in your other question, the correct array key is
methods
and notmethod
. And for the exact same callback (and parameters), you can supply an array of methods. (See below examples) -
I’m guessing you got the error
404
because you tried to make a POST request to your custom endpoint, in which case the error is expected since you haven’t added an endpoint for POST requests. So add it, like the examples here or see below:register_rest_route( 'theme/v1', '/pages', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_page_data' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'get_page_data' ), ), ) ); // or provide an array of methods: register_rest_route( 'theme/v1', '/pages', array( 'methods' => [ 'GET', 'POST' ], 'callback' => array( $this, 'get_page_data' ), ) );