Last active
August 18, 2017 13:47
-
-
Save bueltge/14c8eb4397352156de86ede1f9020a3f to your computer and use it in GitHub Desktop.
Get WP Posts via REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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