Skip to content Skip to sidebar

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are working on a development site and encountering an issue where certain links do not fall back to the default language on the German version of the page, resulting in a 404 error. Some links like 'ASTM C365' correctly fall back, but others like 'ASTM C109' and 'ASTM C170' do not.
Solution:
We recommend you apply the following workaround to address the fallback issue:
1. Edit the file at

/wp-content/plugins/sitepress-multilingual-cms/classes/query-filtering/class-wpml-query-parser.php

2. Replace the code on line 419 with:

$requested_page = get_page_by_path($q->query_vars['name'], OBJECT, $first_post_type);

3. After line 458, add a new function to handle the page path and post type processing more effectively. This function also includes improved caching mechanisms and SQL queries to ensure correct page retrieval based on the language settings.

After implementing these changes, please verify if the language switcher functions as expected.

If this solution does not resolve your issue or seems irrelevant due to updates or differences in your setup, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If problems persist, please open a new support ticket.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 0 reply, has 1 voice.

Last updated by Osama Mersal 5 months ago.

Assisted by: Osama Mersal.

Author Posts
February 16, 2025 at 10:40 am #16710379

dawnV

Hello Osama,

After uploading and enabling the theme , there are some error occured so i have to upload the theme again after removing the peice of dependency from the theme. Can you please disable the theme so i can upload it again or I can provide the zip file of theme.

Thanks.

February 16, 2025 at 11:05 am #16710428

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for trying to replicate the issue. Unfortunately, the sandboxes don't have FTP, so I created a hidden link">new sandbox for you.

Best regards,
Osama

February 16, 2025 at 12:30 pm #16710650

dawnV

Hi Osama,

I've uploaded the Permalink Manager plugin and modified the permalink structure. However, after creating a new page in English and attempting to open it in French or another language, I get a "Page Not Found" error instead of the expected fallback page.

"hidden link"

Additionally, I've uploaded the theme but haven't activated it yet.

Thanks.

February 16, 2025 at 12:52 pm #16710700

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for your update. I checked the mentioned page, and the fallback works correctly. (hidden link)

Please ensure that the Permalinks Manager settings on the sandbox are the same as those on your site.

Thanks for your cooperation

Best regards,
Osama

February 17, 2025 at 11:35 am #16713734

dawnV

Hi Osama,

For creating a new page , it is working fine in our environment as well.

we have created the same environment as our staging environemt so you can enable and disable the plugins as per your need. please provide private links so i can provide the links and credentials.
We are facing the 404 page for fallback in our existing migrated content so can you please help us regarding this issue.

Thanks.

February 17, 2025 at 12:07 pm #16713989

dawnV

New dev environment in which you can make changes. but please do not make any changes in staging environment.

February 17, 2025 at 12:11 pm #16714002

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

I've enabled the private reply, so you can share the new environment access details safely.

Best regards,
Osama

February 17, 2025 at 4:13 pm #16715587

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for the access details. I consulted our second-tier support team and will update you as soon as they reply.

Thanks,
Osama

February 18, 2025 at 4:36 pm #16720219

dawnV

Hi Osama and team,

I am also facing another main issue is like when i am on page "hidden link"

in this german page , there is a link "VHS-Systeme mit hoher Dehnrate" into the page. so this linked page ( VHS ) is available in only english and chinese langauge , but when i click on this link it is redirect to "chinse language(zh-hans)" page. means default fallback pages not working, it should open the page in German(de) url with english content.

Thanks.

February 19, 2025 at 8:40 am #16722723

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for your update. I'll let our 2nd tier support team know about this issue and update you as soon as they reply.

Best regards,
Osama

February 21, 2025 at 2:44 pm #16734219

Osama Mersal
WPML Supporter since 02/2020

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Our 2nd tier support team has found a workaround for the issue. I tried to activate your theme to apply it, but after activating it, there has been a fatal error. (hidden link)

Please activate your theme and permalinks manager plugin and let me know.

Here is the workaround if you want to test it by yourself:

1- Edit the (/wp-content/plugins/sitepress-multilingual-cms/classes/query-filtering/class-wpml-query-parser.php) file
2- Replace the code in line 419

$requested_page = $this->wpml_get_page_by_path( $q->query_vars['name'], OBJECT, $first_post_type );

With

$requested_page = get_page_by_path( $q->query_vars['name'], OBJECT, $first_post_type );

3- Add the following function after line 458

	public function wpml_get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
		global $wpdb;

		$last_changed = wp_cache_get_last_changed( 'posts' );
		$hash         = md5( $page_path . serialize( $post_type ) );
		$cache_key    = "wpml_get_page_by_path:$hash:$last_changed"; // Updated cache key
		$cached       = wp_cache_get( $cache_key, 'post-queries' );

		if ( false !== $cached ) {
			// Special case: '0' is a bad `$page_path`.
			if ( '0' === $cached || 0 === $cached ) {
				return;
			} else {
				return get_post( $cached, $output );
			}
		}

		// Prepare the page path parts
		$page_path = rawurlencode( urldecode( $page_path ) );
		$page_path = str_replace( '%2F', '/', $page_path );
		$page_path = str_replace( '%20', ' ', $page_path );
		$parts     = explode( '/', trim( $page_path, '/' ) );
		$parts     = array_map( 'sanitize_title_for_query', $parts );
		$escaped_parts = esc_sql( $parts );

		$in_string = "'" . implode( "','", $escaped_parts ) . "'";

		// Handle post type(s)
		if ( is_array( $post_type ) ) {
			$post_types = $post_type;
		} else {
			$post_types = array( $post_type, 'attachment' );
		}

		$post_types          = esc_sql( $post_types );
		$post_type_in_string = "'" . implode( "','", $post_types ) . "'";

		// SQL to join icl_translations for WPML and filter by source_language_code
		$sql = "
		SELECT p.ID, p.post_name, p.post_parent, p.post_type
		FROM $wpdb->posts p
		LEFT JOIN {$wpdb->prefix}icl_translations t ON p.ID = t.element_id
		WHERE p.post_name IN ($in_string)
		AND p.post_type IN ($post_type_in_string)
		AND (t.source_language_code IS NULL OR t.source_language_code = '')
	";

		$pages = $wpdb->get_results( $sql, OBJECT_K );

		$revparts = array_reverse( $parts );

		$foundid = 0;
		foreach ( (array) $pages as $page ) {
			if ( $page->post_name == $revparts[0] ) {
				$count = 0;
				$p     = $page;

				/*
				 * Loop through the given path parts from right to left,
				 * ensuring each matches the post ancestry.
				 */
				while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
					++$count;
					$parent = $pages[ $p->post_parent ];
					if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
						break;
					}
					$p = $parent;
				}

				if ( 0 == $p->post_parent && count( $revparts ) === $count + 1 && $p->post_name == $revparts[ $count ] ) {
					$foundid = $page->ID;
					if ( $page->post_type == $post_type ) {
						break;
					}
				}
			}
		}

		// Cache the result (hits and misses)
		wp_cache_set( $cache_key, $foundid, 'post-queries' );

		if ( $foundid ) {
			return get_post( $foundid, $output );
		}

		return null;
	}

After that, please check if the language switcher works as expected.

Best regards,
Osama