Skip to content

Instantly share code, notes, and snippets.

@shawnhooper
Created December 2, 2015 04:46
Show Gist options
  • Save shawnhooper/f56e4183f40196417ac5 to your computer and use it in GitHub Desktop.
Save shawnhooper/f56e4183f40196417ac5 to your computer and use it in GitHub Desktop.
Allow WordPress REST API to read a single post, by post status
add_filter( 'user_has_cap', array( __CLASS__, 'allow_rest_to_read_single_archived_module' ), 10, 3 );
/***
* Modify the capabilities of the REST API to view a single
* "module" post if requested with its post ID.
*
* @param $allcaps
* @param $cap
* @param $args
*
* @return mixed An array of granted capabilities
*/
static function allow_rest_to_read_single_archived_module($allcaps, $cap, $args) {
/* Bail if this isn't a rest call */
if (strpos($_SERVER['REQUEST_URI'], '/wp-json/wp/v2/') === false) return $allcaps;
/* Get the current post */
$post = get_post( $args[2] );
/* If CPT isn't "module", get out */
if ($post->post_type !== 'module') return $allcaps;
/* Add the capability to read the post details */
$allcaps["read_posts"] = true;
/* Return the capabilities */
return $allcaps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment