Is it possible to return a list of posts based from the WordPress Rest API v2 based on their schema:
For List of Schemas:
http://v2.wp-api.org/reference/posts/
I want to filter by sticky field, but the same would go for the rest of the fields.
So far I have:
/wp-json/wp/v2/posts?filter[sticky]=true
/wp-json/wp/v2/posts?filter[sticky]=1
Both return the same response as the standard endpoint:
/wp-json/wp/v2/posts
I have read other material such detailing how to sort by meta or custom taxonomies but I don’t believe that’s the same as this.
After going through the documentation and looking and posting issues on the WP-API Github repo, it’s become clear that the
filter[ignore_sticky_posts]
should toggle the expected sorting behaviour, so that sticky posts are either always first (default) or ignored (by usingfilter[ignore_sticky_posts]=true
).However, there’s currently a bug in WP API that makes the
filter[ignore_sticky_posts]
flag unusable.The best way to fix it now would be to create you own custom endpoint to get the data or IDs of all the sticky posts in your database. By looking at the code discussed in this thread and in the WP-API documentation, I think adding the following code to your
functions.php
should do the trick:If you
GET
/wp-json/THEME_NAME/v1/sticky, you should get an array of all your sticky posts.I hope this helps.
In addition to Laust Deleuran’s answer (thanks Laust!), i’ve created an altered version of his script which allows you to use the
embedded
feature of theREST-api
.Although this might not be ‘the cleanest’ solution, it does allow you to fully use the
wp-json
‘s functionality.This will output the sticky
posts
in the sameschema
as a normal/wp-json/wp/v2/posts
query would respond.