Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

GET resources by slug #642

Closed
danielbachhuber opened this issue Nov 26, 2014 · 16 comments
Closed

GET resources by slug #642

danielbachhuber opened this issue Nov 26, 2014 · 16 comments

Comments

@danielbachhuber
Copy link
Member

A pretty common need is to be able to get a post by a slug. What if would it look like if you could get any resource by id or slug?

  • Post - Type would need to be indicated in the route, as posts of two different types can have the same post_name.
  • Term - Taxonomy would need to be indicated in the route, as two terms of different taxonomies can have the same slug.
  • Comment - No such thing as a comment slug.
  • Meta - Meta keys are sometimes unique. We can't rely on it.
  • User - User's slug is the user_nicename which can be relied upon to be unique (as it's used in author URLs) and has an index in the db.
@danielbachhuber
Copy link
Member Author

One pretty big gotcha: Each of these resources with slugs also permit numbers in the name equivalent field, which means the router would need to explicitly distinguish between a supplied id and a supplied slug.

@goblindegook
Copy link

The way I've been doing it is through a custom endpoint, but I toyed with the idea of using a non-valid slug character to signal the use of a slug. For example: GET /posts/12 (id 12) vs GET /posts/~12 (slug "12").

@rmccue
Copy link
Member

rmccue commented Nov 26, 2014

I am hugely in favour of using filters on the collection for this, and using a route with the (unchangeable) ID in it. Slugs can change at any point, so I think they definitely make more sense as filter variables. For the same reason, I'm +1 on removing the old /pages/<slug>

One issue here I think is that there's no equivalent filter for get_page_by_path, which we need (could be wrong though?)

@danielbachhuber
Copy link
Member Author

Slugs can change at any point, so I think they definitely make more sense as filter variables.

Good point. We could punt on formal adoption, offer some suggestions on how developers can easily implement such an endpoint, and then see if anyone gets bitten by it in production.

One issue here I think is that there's no equivalent filter for get_page_by_path, which we need (could be wrong though?)

I was thinking we'd use WP_Query behind the scenes.

@rmccue
Copy link
Member

rmccue commented Nov 26, 2014

I was thinking we'd use WP_Query behind the scenes.

I agree in the general case, but WP_Query doesn't handle this; it's handled as a special-case in WP::parse_request. I think we should accommodate this somehow, whether that's overloading filter to also do it, or a separate endpoint.

@marketingpartnersau
Copy link

Hey guys,

If anyone is interested, here's an angular service I wrote to get posts, custom post types and pages by name (as well as ui-router state to get it):

https://gist.github.com/marketingpartnersau/930c58bd371b76f015a3

@danielbachhuber
Copy link
Member Author

This has been addressed.

For querying by slugs, our pattern will be /wp/posts?slug=<slug>

Related #924

@baffleinc
Copy link

Hmm... hitting /wp-json/posts?slug=why-us returns my 10 most recent blog posts instead of the why us page.. Am I missing something?

@danielbachhuber
Copy link
Member Author

Am I missing something?

Not implemented in develop yet.

@baffleinc
Copy link

Ah ok... what's the best solution at present?

@danielbachhuber
Copy link
Member Author

Use name instead

@baffleinc
Copy link

Yeah...

/wp-json/posts?name=why-us - 10 most recent posts
/wp-json/posts?filter[name]=why-us - empty array

both stopped working after they used to work A-OK. Only since updating to 1.2. I'm not crazy, the page exists: https://www.dropbox.com/s/27ceykrau45de62/Screenshot%202015-04-02%2010.43.58.png?dl=0

@danielbachhuber
Copy link
Member Author

both stopped working after they used to work A-OK.

Oh. Can you open a new issue then? Might be a bug specific to 1.2

@baffleinc
Copy link

Sure. I tried downgrading, uninstalling, reinstalling, turning all plugins off again and having the same problem

As it turns out no filters are working - can you imagine any reason why?

  1. http://www2.health.com.au/wp-json/posts/?filter[name]=why-us - returns empty array
  2. http://www2.health.com.au/wp-json/posts/?filter[type][]=page - returns most recent posts (not pages)
  3. http://www2.health.com.au/wp-json/posts/?filter[page]=3 - returns first page of posts (same as Only access superglobals in a single place #2)

Would really appreciate some help as it has been working fine.

@baffleinc
Copy link

Any idea when the ?slug param will be introduced? just getting empty arrays still :(

@kadamwhite
Copy link
Contributor

@baffleinc Are any other query parameters (like posts?page=2) working? What server are you using? If it's nginx, make sure your try_files directive is specified as described in #1926, it's possible your server can't see the query parameters.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants