Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get home page? #356

Open
ijakparov opened this issue Feb 25, 2018 · 3 comments
Open

How to get home page? #356

ijakparov opened this issue Feb 25, 2018 · 3 comments

Comments

@ijakparov
Copy link

How to get home page using path() not an id() filter?

wp.pages().id(2).... works well
wp.pages().path('/').... said wp.pages(...).path is not a function

@kadamwhite
Copy link
Collaborator

This looks like a bad documentation issue on our end; path is not listed in the accepted parameters for the core pages endpoint, which suggests that it was removed before we merged the REST API and I did not notice to update the documentation here. Thank you for reporting the issue, we'll validate this is no longer supported and update the documentation.

Unfortunately there is no obvious way I can think of to accomplish what you want. The best I could suggest would be to .search() for the title of the homepage, or use .slug() if you know the slug for the page.

@panayotoff
Copy link

You can register custom endpoint and get the homepage from there. See the code they used for wuxt:

add_action('rest_api_init', 'wuxt_front_page_route');


 function wuxt_front_page_route() {
     register_rest_route('wuxt', '/v1/front-page', array(
         'methods'  => 'GET',
         'callback' => 'wuxt_get_front_page'
     ));
 }


 function wuxt_get_front_page( $object ) {

     $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );

     $frontpage_id = get_option( 'page_on_front' );
     if ( $frontpage_id ) {
         $request  = new WP_REST_Request( 'GET', '/wp/v2/pages/' . $frontpage_id );
     }

     $response = rest_do_request( $request );
     if ($response->is_error()) {
         return new WP_Error( 'wuxt_request_error', __( 'Request Error' ), array( 'status' => 500 ) );
     }

     $embed = $object->get_param( '_embed' ) !== NULL;
     $data = rest_get_server()->response_to_data( $response, $embed );

     return $data;

 }

Then you can get the homepage by calling WPAPIInstance.namespace('wuxt/v1').homepage()

I hope it helps

@kadamwhite
Copy link
Collaborator

Thank you for the example! @panayotoff would you be comfortable with me adding this snippet to the docs as a FAQ item guide?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants