6

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.

1 Answer 1

1

You have rewrite_rules in wp_options table. From there your app can have idea of what the wordpress is going to rewrite in future and how it is going to rewrite after index.php. You can use regex in JS with wp_options data to customise your app response.

UPDATE

Here is block of code I needed to determine the nature of current page/post to dynamically use different template based on the return-

if(!function_exists('get_nature_of_post')):
function get_nature_of_post() { 
  global $wp_query;
  $wpq = json_decode(json_encode($wp_query),true);
  $check = array("is_single","is_page","is_archive","is_author","is_category","is_tax","is_search","is_home","is_404","is_post_type_archive");

  $page_identifiers = array_intersect_key($wpq,array_flip($check));
  $page_identifiers = array_filter($page_identifiers);
  $keys = array_flip(array_keys($page_identifiers));

  $case['home'] = array_flip(array('is_home'));
  $case['search'] = array_flip(array('is_search'));
  $case['archive'] = array_flip(array('is_archive','is_post_type_archive'));
  $case['taxonomy'] = array_flip(array('is_archive','is_tax'));
  $case['single'] = array_flip(array('is_single'));
  $case['page'] = array_flip(array('is_page'));

  $home = !array_diff_key($case['home'], $keys) && !array_diff_key($keys, $case['home']);
  $archive = !array_diff_key($case['archive'], $keys) && !array_diff_key($keys, $case['archive']);
  $search = !array_diff_key($case['search'], $keys) && !array_diff_key($keys, $case['search']);
  // var_dump($archive);
  $taxonomy = !array_diff_key($case['taxonomy'], $keys) && !array_diff_key($keys, $case['taxonomy']);
  // var_dump($taxonomy);
  $single = !array_diff_key($case['single'], $keys) && !array_diff_key($keys, $case['single']);
  // var_dump($single);
  $page = !array_diff_key($case['page'], $keys) && !array_diff_key($keys, $case['page']);
  // var_dump($page);

  switch (!false) {
    case $archive: return 'archive'; break;
    case $taxonomy: return 'taxonomy'; break;
    case $single: return 'single'; break;
    case $page: return 'page'; break;
    case $search: return 'search'; break;
    case $home: return 'home'; break;
    default: return false;
  }
}
endif;
9
  • Thanks, How can I check a url against rewrite_rules? does this mean that I have to send the whole rules to the client and do regex on client side? even after I found the match, it is identified by a key: ` s: 11: "^wp-json/?$";` , How do I relate that number to the page I'm looking for?
    – Ramtin Gh
    Commented Jan 19, 2017 at 6:15
  • First create a function which will return you the format of permalink it will possibly rewrite... Like SITE_URL/page, SITE_URL/post, SITE_URL/Custom_Post(Archive), SITE_URL/Custom_Post/post, SITE_URL/CPT_taxonomy/taxonomy_name. Among these you will have problem in recognizing normal page/post. Commented Jan 19, 2017 at 10:20
  • I will update a function... wait... Commented Jan 19, 2017 at 10:22
  • You can implement get_post_type() as return when it is archive in the above function I updated. Commented Jan 19, 2017 at 10:53
  • Dear @Vishal, thanks for the effort, but there is no current post, all the app has is a url which needs to be resolved in an api endpoint, so basically we don't have a wp_query in an endpoint on which only a url is requested.
    – Ramtin Gh
    Commented Jan 19, 2017 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.