Skip to content

Instantly share code, notes, and snippets.

@bueltge
Last active August 18, 2017 13:47
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 bueltge/14c8eb4397352156de86ede1f9020a3f to your computer and use it in GitHub Desktop.
Save bueltge/14c8eb4397352156de86ede1f9020a3f to your computer and use it in GitHub Desktop.
Get WP Posts via REST API
<?php
class foo {
/**
* Return sites of MU.
* $sites object
*/
public function get_sites() {
$sites = get_sites();
return $sites;
}
/**
* Parse posts.
*/
public function get_remote_posts() {
$posts = get_transient( 'remote_posts' );
if ( empty( $posts ) ) {
$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );
if ( is_wp_error( $response ) ) {
return array();
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if ( empty( $posts ) ) {
return array();
}
set_transient( 'remote_posts', $posts, HOUR_IN_SECONDS );
}
return $posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment