Resolved

Reported for: WPML Multilingual CMS 3.1.9.5

Resolved in: 3.2

Symptoms

When one or more parent pages have different children pages with the same slug, opening one of the children redirects to the wrong page.

Re-using a slug in children pages seems to cause wrong redirection on the front-end.

Example:

  1. Page A
    1. child-aa
    2. child-ab
  2. Page B
    1. child-aa (unique page, but same slug as 1.1)
    2. child-bb

Workaround

The issue will be fixed with WPML 3.2.
Meanwhile you can use the following workaround.

Please note that this only works with duplicated child slugs, as long as parents are not using the same name as children.

  1. Make a full backup of your website. If possible, try this first in a non production copy of your website.
  2. Open sitepress.class.php
  3. Go to line 7804: it should contains only // Adjust page name
  4. Replace this block of code (line from 7804 to 7828:
    // Adjust page name
    if ( isset( $q->query_vars[ 'pagename' ] ) && !empty( $q->query_vars[ 'pagename' ] ) ) {
    	// find the page with the page name in the current language.
    	$pagename = explode ( '/', $q->query_vars[ 'pagename' ] );
    	$pagename = array_pop ( $pagename );
    	$pid = $wpdb->get_var( $wpdb->prepare( "
    					SELECT ID
    					FROM $wpdb->posts p
    					JOIN {$wpdb->prefix}icl_translations t
    					ON p.ID = t.element_id AND t.element_type='post_page'
    					WHERE p.post_name=%s AND t.language_code = %s
    					", $pagename, $current_language ) );
    	if ( $pid ) {
    		$q->query_vars[ 'page_id' ] = $pid;
    		// We have found the page id
    		unset( $q->query_vars[ 'pagename' ] );
    		if ( $q->query_vars[ 'page_id' ] == get_option( 'page_for_posts' ) ) {
    			// it's the blog page.
    			$q->is_page       = false; // $wp_query->is_page       = false;
    			$q->is_home       = true; // $wp_query->is_home       = true;
    			$q->is_posts_page = true; // $wp_query->is_posts_page = true;
    		}
    	}
    }

    With that block:

    // Adjust page name
    if ( isset( $q->query_vars[ 'pagename' ] ) && !empty( $q->query_vars[ 'pagename' ] ) ) {
    	// find the page with the page name in the current language.
    	$pid = $wpdb->get_var( $wpdb->prepare( "
    					SELECT ID
    					FROM $wpdb->posts p
    					JOIN {$wpdb->prefix}icl_translations t
    					ON p.ID = t.element_id AND element_type='post_page'
    					WHERE p.post_name=%s AND t.language_code = %s
    					", $q->query_vars[ 'pagename' ], $current_language ) );
    	if ( $pid ) {
    		$q->query_vars[ 'page_id' ] = $pid;
    		// We have found the page id
    		unset( $q->query_vars[ 'pagename' ] );
    		if ( $q->query_vars[ 'page_id' ] == get_option( 'page_for_posts' ) ) {
    			// it's the blog page.
    			$q->is_page       = false; // $wp_query->is_page       = false;
    			$q->is_home       = true; // $wp_query->is_home       = true;
    			$q->is_posts_page = true; // $wp_query->is_posts_page = true;
    		}
    	}
    }
    
  5. Save
  6. Reload the website

5 Responses to “Wrong page redirections when 2 child pages use the same slug”

  1. Wow. Obviously a serious bug. This really needs to get published in an update right away.