[API] Begin with REST API – Display others blogs latest posts

https://rudrastyh.com/wordpress/rest-api-get-posts.html

Ví dụ 1:

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\header.php

<?php  
				// connect to the website endpoint with wp_remote_get() function
				// pass params as URL query args, full parameter list is here https://developer.wordpress.org/rest-api/reference/posts/
				// at this moment you can use any parameter with Context: View
				// it would be strange if you can fetch drafts or private posts, right?
				$response = wp_remote_get(add_query_arg(
				  array(
				    'per_page' => 2,
				  ), 'http://localhost/wordpress/wp-json/wp/v2/posts')
				);
				if (!is_wp_error($response) && $response['response']['code'] == 200) {
				  $remote_posts = json_decode($response['body']); // our posts are here
				  foreach ($remote_posts as $remote_post) {
				    // display post titles and excerpts
				    echo '<div>' . $remote_post->title->rendered . '</div>';
				    // need more parameters? print_r( $remote_post )
				  }
				}
			?>

Ví dụ 2:

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\header.php

<?php  
// trying to get value from the cache
if (false == $allposts = get_transient('misha_remote_cache')) {
  // it will be the array with all posts
  $allposts = array();
  // get the first website latest posts
  $blog1 = wp_remote_get('https://ma.tt/wp-json/wp/v2/posts?per_page=2');
  if (!is_wp_error($blog1) && $blog1['response']['code'] == 200) {
    $remote_posts = json_decode($blog1['body']); // our posts are here
    foreach ($remote_posts as $remote_post) {
      // I decided to create array like $allposts[1504838841] = Object
      $allposts[strtotime($remote_post->date_gmt)] = $remote_post;
    }
  }
  // get the second website latest posts
  $blog2 = wp_remote_get('https://css-tricks.com/wp-json/wp/v2/posts?per_page=2');
  if (!is_wp_error($blog2) && $blog2['response']['code'] == 200) {
    $remote_posts = json_decode($blog2['body']); // our posts are here
    foreach ($remote_posts as $remote_post) {
      $allposts[strtotime($remote_post->date_gmt)] = $remote_post;
    }
  }
  // sort array by keys in descending order
  krsort($allposts);
  // store cache
  set_transient('misha_remote_cache', $allposts, 60); // for 1 minute in this example
}
// print posts
foreach ($allposts as $remote_post) {
  echo '<h2>' . $remote_post->title->rendered . '</h2><p>' . $remote_post->date_gmt . '</p>';
}
?>

Begin with REST API – Display others blogs latest posts

Updated on Jul 23rd, 2019 in

As you maybe heard, REST API allows to interact with your WordPress website from outside and it could be another website or mobile app.

Latest Posts from Matt Mullenweg’s Blog

Matt is the founder of WordPress. Oh, really, is that possible to get his posts? 😲 Let’s look at a very very simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// connect to the website endpoint with wp_remote_get() function
// pass params as URL query args, full parameter list is here https://developer.wordpress.org/rest-api/reference/posts/
// at this moment you can use any parameter with Context: View
// it would be strange if you can fetch drafts or private posts, right?
$response = wp_remote_get( add_query_arg( array(
	'per_page' => 2
), 'https://ma.tt/wp-json/wp/v2/posts' ) );
 
if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
 
	$remote_posts = json_decode( $response['body'] ); // our posts are here
	foreach( $remote_posts as $remote_post ) {
 
		// display post titles and excerpts
		echo '<h2>'. $remote_post->title->rendered . '</h2><p>' . $remote_post->excerpt->rendered . '</p>';
		// need more parameters? print_r( $remote_post )
 
	}
}

If you have experience with Instagram or MailChimp API for example, the above code should be awesomely simple for you.

Posts from Two and More Blogs in Chronological Order

Sometimes you may need to get posts from several blogs, for example for your “Latest WordPress News” widget. It is possible to do with WordPress REST API + HTTP API but transient cache is highly recommended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// trying to get value from the cache
if( false == $allposts = get_transient( 'misha_remote_cache' ) ) {
 
	// it will be the array with all posts
	$allposts = array();
 
	// get the first website latest posts
	$blog1 = wp_remote_get( 'https://ma.tt/wp-json/wp/v2/posts?per_page=2' );
 
	if( !is_wp_error( $blog1 ) && $blog1['response']['code'] == 200 ) {
 
		$remote_posts = json_decode( $blog1['body'] ); // our posts are here
		foreach( $remote_posts as $remote_post ) {
 
			// I decided to create array like $allposts[1504838841] = Object
 			$allposts[ strtotime( $remote_post->date_gmt ) ] = $remote_post;
 
		}
	}
 
	// get the second website latest posts
	$blog2 = wp_remote_get( 'https://css-tricks.com/wp-json/wp/v2/posts?per_page=2' );
 
	if( !is_wp_error( $blog2 ) && $blog2['response']['code'] == 200 ) {
 
		$remote_posts = json_decode( $blog2['body'] ); // our posts are here
		foreach( $remote_posts as $remote_post ) {
 
 			$allposts[ strtotime( $remote_post->date_gmt ) ] = $remote_post;
 
		}
	}
 
	// sort array by keys in descending order
	krsort( $allposts );
 
	// store cache
	set_transient( 'misha_remote_cache', $allposts, 60 ); // for 1 minute in this example
 
}
 
// print posts
foreach( $allposts as $remote_post ) {
	echo '<h2>'.$remote_post->title->rendered.'</h2><p>' . $remote_post->date_gmt . '</p>';
}

How to Completely Disable REST API /wp-json on your Website

Well, what if you do not want someone to interact with your website API and to get your posts without permission? 😂 So, in that case you can easily disable /wp-json/

This code works for WordPress 4.7 and higher, no plugin required.

add_filter( 'rest_authentication_errors', 'misha_no_rest_api_4_7' );
 
function misha_no_rest_api_4_7( $access ) {
 
	return new WP_Error( 'rest_cannot_access', 'Soooooryyyy', array( 
		'status' => 403 
	) );
 
}

In the next tutorial about REST API we will talk about authorization.

Last updated