I've been looking all over the web for something like what I need and I haven't find anything resemble not even similar, the thing is this:

I got a Page Template to a simple page post type, let's say 'Fruits' and then in this template I am calling a Web Services that will bring back all fruits with a certain description, the url of this is www.mydomain.com/fruits/

I am not populating Wordpress database with this information but taking it from an external service and I want to navigate the content with it details in Wordpress templates design, so what I need is be able to click on 'orange' so I would be creating a link like this:

www.mydomain.com/fruits/orange/ ---> This would be my permalink that is not registered

This non-existing permalink will redirect me immediately to the 404 Template page and it will be categorized in the wp_query object as '404' = true, I want to avoid wordpress from processing it as a 404 and tweak it inners to send this request to a custom-template that will display the details of orange.

Please If you got any idea which hook can I use that doesn't involve WP_Rewrite (which I tried all kind of combinations with no success) I will be forever grateful.

Thanks in advance.

  • what is to distinguish what should be a genuine 404 from these "false" 404 pages? an easier method would be to prefix these pages with something to separate them from other page post type. wp_rewrite would be the handle this in that case.
    – Milo
    Commented Jul 31, 2013 at 18:41
  • The thing is that this information does not exist in wordpress it works perfectly with the template adaption pulling the data from external source but since there are about 4k fruits I can't just create a custom post type for each one and populate it under request, this would be to much unnecessary data storage, that If I can avoid this would be great. Commented Jul 31, 2013 at 18:49
  • you didn't answer my first question. I didn't suggest creating posts for each one, simply a unique prefix, the way WordPress uses tag or category.
    – Milo
    Commented Jul 31, 2013 at 19:01

2 Answers 2


As I hinted at in my comment, I would use the rewrite API to handle this, specifically by adding a rewrite endpoint. That way you don't have to deal with working out what's an actual 404, and what's a request for one of your dynamically generated pages. It doesn't give you exactly the URL structure you have in your question, but I think it's a reasonable compromise in this case.

First we add the rewrite endpoint. In this example I've used type, so your URLs would be /fruit/type/orange/:

function wpa_rewrite_endpoint(){
    add_rewrite_endpoint( 'type', EP_PAGES );
add_action( 'init', 'wpa_rewrite_endpoint' );

Now within your template, you can check if type has been set and you'll know to fetch that keyword from your webservice:

if( $requested_type = get_query_var( 'type' ) ){
    // pass $requested_type to webservice

You also might want to look into the Transients API for caching the data locally, if you get a lot of hits and the extra http requests are slowing things down.

  • Thanks Milo, I will give it a try and get back to you :) Commented Jul 31, 2013 at 21:29
  • don't forget to flush rewrite rules after adding this, by visiting the permalinks settings page, or via flush_rewrite_rules
    – Milo
    Commented Jul 31, 2013 at 21:44
  • This partially works, is there a way to add this endpoint to the root, I tried EP_ALL but is not working on www.domain.com/fruit/orange it doesn't find fruit, thanks :) Commented Jul 31, 2013 at 22:38
  • Okay EP_ALL works with root to, seems my last permalink refresh wasn't effective, now is working, thanks Milo! Commented Aug 1, 2013 at 0:05

If you have no other option and have to use 404 redirection. You would use template_redirect action hook.

function custom_404_redirect()
    if( is_404() )
    // wp_redirect(site_url()); redirect to another page or 
        include( get_template_directory() . '/custom-404.php' ); // use a template
add_action( 'template_redirect', 'custom_404_redirect' );
  • Thanks for the suggestion, If I can't find the best way to do it, I think this will work. Commented Jul 31, 2013 at 21:30
  • 1
    if you use this method you'll still need to figure out what's a legitimate 404 and what's one of your webservice pages so you can send a 200 header to override the 404, otherwise these pages will never get indexed.
    – Milo
    Commented Jul 31, 2013 at 21:36

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.