Update:Really short version
How do I get $wp_query
for a given url which can be anything (single, page archive, custom post type, author, year, month,…)?
TL;DR
I need a function which can return this info (as a REST API endpoint) based on a given url:
request:'category/Uncategorized'
response:
{
type:"category",
category_name:"uncategorized"
}
request:'books/harry-potter'
response:
{
type:"single",
post_type:"books",
name:"harry-potter"
}
So my app will know which template to use and what sort of data to expect.
The long story
I’m writing a WordPress theme with ReactJS,
I have my (known) routes set up with react-router,
But I need to detect what sort of content I should expect if the requested url is not known at the present.
In case url is GET ed by browser(not as ajax call), I have my wordpress template file inject all the required data in the page (with $wp_query->query_vars
), So with a simple url check I’ll know if data is meant for current url and if so, I’ll use it.
The problem comes in when user navigates through and next pages load by calling respective API endpoints.
It becomes worse if I want to support all permalink structures, So I think I’ll definitely need to resolve URLs in the back-end to check all the possibilities.
So I have a detector js which tries to see if it can detect what kind of resource is requested (by checking if url params contain category
,author
,…) and make the API call, in case it couldn’t, it then will call the detector endpoint in which I need to tell my app what should be rendered in this page.
For example, I’ve currently registered a custom post type named “Team”, so I already know how to handle mywebsite.com/team
& mywebsite.com/team/:slug
, but in case user registers a new post type, say “Books”, my app will have no idea whether books is a page’s slug, post’s slug or a post type archive name.