Skip Navigation

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

Problem:
Client is trying to query child-pages using the post_parent parameter in custom code. When WPML is activated, where the query returns no values when there should be some.

Solution:
We recommend the following workaround:
1. Add this code to your theme's functions.php file:

add_filter('wpml_pre_parse_query', function($q){
    if (empty($q->get('wpml_do_not_adjust_name'))) {
        return $q;
    }
    
    $name = $q->get('name');
    unset($q->query['name']);
    unset($q->query_vars['name']);

    add_filter('wpml_post_parse_query', function($q) use ($name){
        $q->set('name', $name);
        return $q;
    });

    return $q;
});

2. Then, in the WP_Query, pass wpml_do_not_adjust_name parameter as true, for example:

$args = array(
  'wpml_do_not_adjust_name' => true,
  'post_type'              => $post_types,
  'post_parent'            => 59,
  'name'                   => 'child-page-b',
  'no_found_rows'          => true,  // used to improve performance.
  'update_post_meta_cache' => false, // used to improve performance.
  'update_post_term_cache' => false, // used to improve performance.
);

Please note that after making these adjustments, WPML will stop translating the slug to the secondary language, so it won't display the translated post in the second language. This is because you are passing the static slug in the query and do not want WPML to interfere with this.

For queries in different languages, adjust the

post_parent

and

name

parameters to the translated versions of the parent and child pages, respectively.

We have escalated the case, but there is no clear ETA for it to be added in a near future release. You can use the above-mentioned workaround for the time being.

We also advise checking the WPML Hooks documentation for further customization.

If this solution is irrelevant to your case, either because it's outdated or not applicable, 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 the issue persists, 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 10 replies, has 2 voices.

Last updated by Waqas Bin Hasan 11 months, 1 week ago.

Assisted by: Waqas Bin Hasan.

Author Posts
February 15, 2024 at 8:41 am #15305532

christopherA-19

For some reason I'm not able to query pages in the same way using the post_parent parameter when WPML is activated. The query returns no values when there should be some.

February 15, 2024 at 9:46 am #15305723

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Reference to our recent chat, I've prepared a sandbox site at hidden link .

I've also created a few pages Parent and Child (see attached). These are also translated.

You can login using the above mentioned link and can use File Manager plugin (available in Plugins) to edit theme files, where you can add your query and prepare the example.

Please let me know when you're ready.

Regards.

pages.jpg
February 16, 2024 at 7:53 am #15310250

christopherA-19

Thanks for setting up the instance. I've been able to duplicate the issue on your instance.

I've made some changes to the page.php and functions.php file to duplicate the conditions under which this issue occurs.

Now when you go to this parent page: hidden link
With the "WPML Multilingual CMS" plugin activated, you'll see nothing special with the page. When you deactivate "WPML Multilingual CMS", you'll then see the title of a subpage here (see screenshot). It comes from the query I placed in the page.php template. When you reactivate the plugin, you'll see it disappear again.

Somehow the "WPML Multilingual CMS" plugin is interfering with the query I put in page.php. Any ideas why?

February 16, 2024 at 7:54 am #15310252

christopherA-19

Screenshot attached:

Screenshot 2024-02-16 at 2.50.59 PM.jpg
February 16, 2024 at 11:23 am #15311239

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the updates, I can see how you described it.

I am working on it and 'll also discuss the matter with the team. Please note that due to almost end of my shift and weekend ahead, I'll be able to get back to you on Monday as earliest.

Have a great weekend.

February 19, 2024 at 8:06 am #15316251

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your patience and cooperation. I've discussed and escalated the matter to our 2nd tier team for more investigation.

I'll get back to you accordingly.

February 22, 2024 at 5:22 am #15331039

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your patience and cooperation. Our team suggest the following workaround:

1. Add this code in theme's functions.php:

	/**
	 * @link wpmlsupp-11453
	 */
	add_filter('wpml_pre_parse_query', function($q){
		if (empty($q->get('wpml_do_not_adjust_name'))) {
			return $q;
		}
		
		$name = $q->get('name');
		unset($q->query['name']);
		unset($q->query_vars['name']);

		add_filter('wpml_post_parse_query', function($q) use ($name){
			$q->set('name', $name);
			return $q;
		});

		return $q;
	});

2. Then in the WP_Query pass wpml_do_not_adjust_name paramter as true e.g:

$args = array(
  'wpml_do_not_adjust_name' => true,
	'post_type'              => $post_types,
	'post_parent'            => 59,
	'name'                   => 'child-page-b',
	'no_found_rows'          => true,  // used to improve performance.
	'update_post_meta_cache' => false, // used to improve performance.
	'update_post_term_cache' => false, // used to improve performance.
);

NOTE: After making these adjustment WPML will stop translating the slug to secondary language. So it won't display the translated post on 2nd language. Since you are passing the static slug in query we assume you need exactly the post that matching the slug and do not want WPML to interfere with this.

Team further noted:

When you have two posts with the same slug in different post types. WPML tries to resolve the post type to translate the slug in query and this results in getting the CPT post first based on our internal logic.

Without WPML both posts are pulled from DB and one with parent post is displayed based on query. When WPML is active, CPT post is getting pulled and it has no parent thus no post is displayed.

The question is why WPML is not pulling both of the posts, this is because we try to translate the post slug in the query and return our best match.

Team has further escalated the case, however, there's no clear ETA for it to be added in a near future release. As the setup is very unlikely to have same slug in two different post types, you can use the above mentioned workaround for the time being.

I've tested this in the sandbox site, feel free to observe and use in your own site as needed.

February 22, 2024 at 9:42 am #15332285

christopherA-19

Ok, thanks for the workaround. I've had to add it in two locations currently and I'm guessing I may find other queries in our theme that will need it before we release this plugin live.

Is there a place I can track the bug itself so that I know when it has been fixed? I'd like to be able to remove these workarounds as soon as I can.

One other question, is it possible to turn off slug translation in all cases? I don't think we want them. Will that also serve as a workaround for this bug?

February 22, 2024 at 10:23 am #15332545

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Unfortunately there's no such place for now, because the matter has been escalated to our dev team. Usually we create an errata if there are so many reports, so everyone can track and trace. But since this is a very rare case, there's nothing like that.

All I can inform you via this ticket if somehow it isn't archived and the solution is out in the meanwhile.

Please check this guide https://wpml.org/documentation/getting-started-guide/translating-page-slugs/ about slug translations. There a couple of related settings.

February 22, 2024 at 12:12 pm #15333403

christopherA-19

Do you have any thoughts on getting this query to work while on a translated page. For example, using the sandbox instance, if I go to a translated version of the parent page: hidden link
I still want the query to return the same page at the bottom, however, it's now saying "No post found"? How do I get the query to work in this situation?

Screenshot 2024-02-22 at 7.09.54 PM.jpg
February 23, 2024 at 5:08 am #15336021

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Of course that works in the same way, all you need to adjust the parent ID to the translated version of the parent page, and, the child slug to the translated version of the child page, for example:

$args = array(
	'wpml_do_not_adjust_name' => true,
	'post_type'              => $post_types,
	'post_parent'            => 61, // ID OF PARENT PAGE (JAPANESE TRANSLATION)
	'name'                   => 'child-page-b-ja', // SLUG OF CHILD PAGE (JAPANESE TRANSLATION)
	'no_found_rows'          => true,  // used to improve performance.
	'update_post_meta_cache' => false, // used to improve performance.
	'update_post_term_cache' => false, // used to improve performance.
);

Notice the 'post_parent' and 'name' attributes and it works fine on hidden link. Reason is the same as mentioned in the "Note" in my previous message with the workaround.

I'll advise to check WPML Hooks (https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/), so you can perhaps get an idea how to improve the above mentioned (i.e. getting current language, switching language, getting content in a particular language and etc).

February 23, 2024 at 6:24 am #15336057

christopherA-19

Ok, thanks. I'll try that out.