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

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

#### 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 )
				  }
				}
			?>
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNuP4HifeqtPrW0HFY%2F-MPNw7QCB_EIqpktBCjG%2FScreenshot_8.jpg?alt=media\&token=5fa02553-f0b6-40cd-9430-de44b678ed57)

#### Ví dụ 2:

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

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNuP4HifeqtPrW0HFY%2F-MPNx3Q063BGeIP1US0P%2FScreenshot_9.jpg?alt=media\&token=26188be2-e933-4f11-b0aa-90ffde1d8253)

```
<?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&#x20;

1. [Blog](https://rudrastyh.com/)&#x20;
2. [WordPress](https://rudrastyh.com/wordpress)

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 <a href="#latest" id="latest"></a>

Matt is the founder of WordPress. Oh, really, is that possible to get his posts? ![😲](https://s.w.org/images/core/emoji/13.0.1/svg/1f632.svg) 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](https://rudrastyh.com/wordpress/wp_remote_get-instagram-api.html) or [MailChimp API](https://rudrastyh.com/wordpress/using-mailchimp-api.html) 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>';
}
```

I created php file in my website directory and tested everything there. But in this case do not forget to `require('wp-load.php')`.![Get Posts with Rest API from the other blogs](https://rudrastyh.com/wp-content/uploads/2017/09/get-posts-with-rest-api.png)

### How to Completely Disable REST API /wp-json on your Website <a href="#disable_rest_api" id="disable_rest_api"></a>

Well, what if you do not want someone to interact with your website API and to get your posts without permission? ![😂](https://s.w.org/images/core/emoji/13.0.1/svg/1f602.svg) 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 
	) );
 
}
```

![It is how to completely disable REST API in WordPress](https://rudrastyh.com/wp-content/uploads/2017/09/disable-rest-api.png)

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