Since filter has been removed, how to get posts by category slug with same schema as v2/posts? #2949
Comments
The most robust way to handle this would be to first request
then use the ID of the category you get back in
Alternatively you can install the new rest-filter plugin, to reinstate filter; or create a plugin to add a query parameter to collections to handle this specific behavior. But the above will work on all sites, whether or not you have the ability to add a plugin to them. It's true that two requests is less efficient than one, and the additional HTTP request does introduce another point of failure on e.g. an unreliable mobile connection; but relying on the slug is conceptually brittle as slugs are mutable (IDs are the only guaranteed relation), and the slug-to-ID mapping can be cached locally within the client app, removing the need to make the initial query unless the subsequent one fails or you know a slug to have changed. Hope this helps! |
OK. Thanks! I've written our own plugin to deal with some custom types, so I'll add something to do the query. You're quite right about the use of slugs though, but our site is built around them staying pretty solid ;) |
Depending on your environment a client method could also be created to abstract the requests into one developer-facing call; for example, with the wpapi npm package I could write, function getPostsByCategorySlug(slug) {
return site.categories().slug( slug )
.then(function( matchingCats ) {
// matchingCats will be an array of the one post that matches
// the provided slug
if ( ! matchingCats.length ) {
throw new Error( `No category found for slug "${slug}"` );
}
var catID = matchingCats[ 0 ].id;
return site.posts().categories(catID);
});
}
getPostsByCategorySlug( 'some-category-slug' )
.then(function( posts ) {
// Do something with your posts
})
.catch(function( err ) {
// Could not get categories
console.error(err);
}); |
Thanks for that. But while writing a little plugin for this with PHP, I'm a little confused :
But in my plugin, after registering the rest route, when I do a How do I return the same structure as Thanks. Here's what I'm doing in the plugin:
|
Update.: RESOLVED (but no XP-* headers?) Seems I needed to the use the WP_REST_Posts_Controller methods to prepare the data :
|
filter
has been removed, how to get posts by category slug?
Re-opening as the question has changed! @joehoyle any ideas about how to pass those headers through? |
These headers need to be generated by your custom controller, however I'm not sure why you are writing a whole new controller just to support The pagination headers part of the Posts Controller isn't all that reusable as you may have discovered, so the above is I think the best way to solve this. |
Thanks. I just updated the previous comment to show that I now have the correct post schema properties in the response. But no pagination headers yet. |
@joehoyle Could I have an example of how to do this? If I'm going about it the wrong way. All I'm trying to do is get a list of posts by category slug in exactly the same structure as |
@kadamwhite I'm getting a collection, not a single post. ;) I know I can do it with the ID, but I want to create a custom endpoint which supports category slug since I can't use filter any more. |
@joehoyle Where is the documentation for all the rest_* hooks? You mentioned Thank you. |
Hi! We're no longer handling support here on GitHub, so future questions should be posted on WordPress.org. To find filters, you can check the developer.wordpress.org reference. |
I want to filter slug that they have some string, wp/v2/categories/?slug=%home% |
Hi there, I have a category "cinema". |
Prior to 4.7 we could get posts from a category using:
/wp-json/wp/v2/posts?filter[taxonomy]=category&filter[term]=some-category-slug
This no longer works. So how should we go about doing this now?
Thanks.
The text was updated successfully, but these errors were encountered: