13

I'm trying to get all posts by ID's using WP REST API. As per the documentation, we can use filter to use WP Query arguments. Using this with posts end point returns all the posts.

http://demo.wp-api.org/wp-json/wp/v2/posts/?filter[posts__in]=470,469
Prashanth
  • 518
  • 1
  • 8
  • 21

4 Answers4

25

With V2 This is working for me, more butter then adding custom code

http://demo.wp-api.org/wp-json/wp/v2/posts?include[]=470&include[]=469

An other way is to compose the query like this:

http://demo.wp-api.org/wp-json/wp/v2/posts?include=470,469
  • 1
    No need to use two `?includes`, you can use: `http://demo.wp-api.org/wp-json/wp/v2/posts?include[469,470]` – Loosie94 Feb 20 '23 at 19:16
17

For the v2 of WP REST API use this format-

http://demo.wp-api.org/wp-json/wp/v2/posts?include[]=470&include[]=469

If you want custom coding then,

You can retrieve single post by id like

http://demo.wp-api.org/wp-json/wp/v2/posts/?filter[p]=470

But as per support its will not work for multiple post.

https://github.com/WP-API/WP-API/issues/1368

So you can ran a loop and get one by one.

But for multiple you have to put an function

add_filter('rest_query_vars', 'custom_rest_query_vars');
function custom_rest_query_vars($query_vars) {
  $query_vars = array_merge( $query_vars,    array('post','post__in','type','id') );
  return $query_vars;
}

Then you have to run

+filter[post__in][]=470&filter[post__in][]=469
Hemel
  • 431
  • 3
  • 15
-1

Update 2023

The easiest and fastest way is this

https://example.com/wp-json/wp/v2/posts/<id> 

more information developer wordpress

S.Saderi
  • 4,755
  • 3
  • 21
  • 23
-1

I want to add with the current answer above. To retrieve a single post form outside through API you can use

https://example.com/wp-json/wp/v2/posts/450

where Post ID = 450.

infomasud
  • 2,263
  • 1
  • 18
  • 12