Skip to content

Commit

Permalink
add a simple method get post by type and postname (no require authent…
Browse files Browse the repository at this point in the history
…ication and filter params)
  • Loading branch information
root committed Sep 23, 2014
1 parent fc2da56 commit 3c3399c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/class-wp-json-posts.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function register_routes( $routes ) {
'/posts/(?P<id>\d+)/revisions' => array(
array( $this, 'get_revisions' ), WP_JSON_Server::READABLE
),

'/posts/(?P<type>\w+)/(?P<path>[a-z\-]+)' => array(
array( $this, 'get_post_by_type_and_path'), WP_JSON_Server::READABLE
),

// Comments
'/posts/(?P<id>\d+)/comments' => array(
Expand Down Expand Up @@ -202,6 +206,21 @@ public function get_posts( $filter = array(), $context = 'view', $type = 'post',
return $response;
}

/**
* Retrieve a simple post by path name
*
* @param string $path
*/
public function get_post_by_type_and_path( $type, $path, $context = 'view' ) {

$post = get_page_by_path( $path, ARRAY_A, $type);
if ( empty( $post ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}

return $this->get_post( $post['ID'], $context );
}

/**
* Check if we can read a post
*
Expand Down

0 comments on commit 3c3399c

Please sign in to comment.