WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm setting up a website for a client where they want several different sections, like so:

Regular Site (/)
  - Section 1 (/section_1/)
  - Section 2 (/section_2/)
  - Section X

The regular site will have all of the main content, but I want to be able to override certain pages if they exist in the specified section.

Let's say the user visits domain.com/section_1/features. If a /section_1/features page exists, I would like to display that; however, if the page does not exist, I would like it to display /features without redirecting or changing the URL in the address bar. It should be completely seamless and behind the scenes.

I think I need to add a function to the parse_request hook, something like:

function check_section_page( &$wp ) {
    $requested_path = $wp->request;

    if ( ! get_page_by_path($requested_path) ) {
        $new_requested_path = preg_replace("/^(.*?)\//", "", $requested_path);
        // do something here to make it read $new_requested_path

add_filter("parse_request", "check_section_page");

It runs the function properly, and echoing out $new_requested_path shows that it does have the new required page. I've been fighting with this for about 2 hours and my head's done in. Help?

share|improve this question
up vote 1 down vote accepted

I managed to solve the problem. Here's how to do it, in case anyone else ever needs this solution.

** Updated Mar 10 3:30am EST **

The previous function that I had here was displaying the page properly, but was returning a 404 error code. I've replaced the bad code example with a working one that does not return a 404.

function check_section_page( $posts ) {

    if (is_admin()) { return $posts; }

    global $wp, $wp_query;

    $requested_path = $wp->request;

    if ( $requested_path == "" ) { return $posts; }

    preg_match("/^([a-z\-]+)\//", $requested_path, $matches);

    if ( count($matches) > 1 ) {
        if ( get_page_by_path( $matches[1] ) ) { $section = $matches[1]; }

    $wp_query->set("saleslink_section", $section);

    $this_page = get_page_by_path( $requested_path );
    $new_requested_path = preg_replace("/^(.*?)\//", "", $requested_path);
    $requested_page = get_page_by_path($new_requested_path);

    if ( $requested_page->ID and ! $this_page->ID ) {

        $posts = NULL;

        $posts[] = $requested_page;
        $wp_query->is_page = true;
        $wp_query->is_singular = true;
        $wp_query->is_home = false;
        $wp_query->is_archive = false;
        $wp_query->is_category = false;


    return $posts;


add_filter("the_posts", "check_section_page");

The remove_filter line will turn off the automatic-url-guessing-redirect behaviour of Wordpress, when a requested URL does not exist and a similar one does and it redirects to the similar URL.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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