Skip to content

Instantly share code, notes, and snippets.

@rachelbaker
Created May 4, 2014 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelbaker/74616f2162ce0bb7769f to your computer and use it in GitHub Desktop.
Save rachelbaker/74616f2162ce0bb7769f to your computer and use it in GitHub Desktop.
<?php
/**
* Retrieve a meta for a post.
*
* @uses get_post_custom()
*
* @param int $id Post ID
* @param string $key
* @return array Post meta fields and values
*/
public function get_post_meta( $id, $context = 'view' ) {
$id = (int) $id;
if ( empty( $id ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}
$post = get_post( $id, ARRAY_A );
$post_meta = (array) get_post_custom( $id );
if ( empty( $post['ID'] ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}
if ( empty( $post_meta ) ) {
return new WP_Error( 'json_post_invalid_meta', __( 'Invalid post_meta.' ), array( 'status' => 404 ) );
}
if ( ! $this->check_read_permission( $post ) ) {
return new WP_Error( 'json_user_cannot_read', __( 'Sorry, you cannot read this post.' ), array( 'status' => 401 ) );
}
// Link headers (see RFC 5988)
$response = new WP_JSON_Response();
$response->header( 'Last-Modified', mysql2date( 'D, d M Y H:i:s', $post['post_modified_gmt'] ) . 'GMT' );
$response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) );
$response->set_data( $post_meta );
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment