Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. function rest_posts_by_id(WP_REST_Request $req) {
  2. $postIds = $req->get_param('post_ids');
  3. $postIds = explode(',', $postIds);
  4.  
  5. $posts = get_posts(array( 'post__in' => $postIds ));
  6. array_walk($posts, function(&$item) {
  7. $item->thumbnail = get_the_post_thumbnail_url($item->ID);
  8. });
  9.  
  10. return new WP_REST_Response($posts, 200);
  11. }
  12. add_action('rest_api_init', function() {
  13. register_rest_route('myapi/v1', '/posts', array(
  14. 'methods' => WP_REST_Server::READABLE,
  15. 'callback' => 'rest_posts_by_id',
  16. 'permission_callback' => function() {
  17. return true;
  18. }
  19. ));
  20. });
  21.  
  22. {"code":"rest_cannot_access","message":"Only authenticated users can access the REST API.","data":{"status":401}}
Add Comment
Please, Sign In to add comment