Skip to content Skip to sidebar

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

Problem:
You are trying to get hreflang tags to render on your public custom post that has a custom post status, but they are not appearing as expected.
Solution:
We recommend adding the following code to your functions.php file to fix the issue with post status affecting WPML hreflangs:

&#x2F;**<br /> * Fix post status issue for WPML hreflangs.<br /> * <br /> * @link wpmlsupp-11719<br /> *&#x2F;<br />add_filter('wpml_head_langs', function($args){<br />    $change_post_status = function( $post_status ) {<br />        //Change dormant to actual custom post status.<br />        if ($post_status === 'dormant') {<br />            return 'publish';<br />        }<br /><br />        return $post_status;<br />    };<br /><br />    add_filter('get_post_status', $change_post_status);<br /><br />    add_filter('wpml_hreflangs', function($args) use ($change_post_status){<br />        remove_filter('get_post_status', $change_post_status);<br />        return $args;<br />    });<br /><br />    return $args;<br />});

After implementing this code, please verify if the hreflang tags are correctly rendering for your posts.

If this solution does not resolve your issue or seems irrelevant due to being outdated or not applicable to your case, we highly recommend checking related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If further assistance is needed, 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 4 replies, has 2 voices.

Last updated by Osama Mersal 1 year, 2 months ago.

Assisted by: Osama Mersal.

Author Posts
May 13, 2024 at 1:36 pm #15621159

Paradigm

I am trying to: get hreflang tags to render on my public custom post that has a custom post status

Link to a page where the issue can be seen:
hidden link

I expected to see:
<link rel="alternate" hreflang="en" href="hidden link" />
<link rel="alternate" hreflang="ja" href="hidden link" />
<link rel="alternate" hreflang="x-default" href="hidden link" />

Instead, I got:
no hreflang tags. (The example above has custom post status "dormant")

May 13, 2024 at 2:02 pm #15621273

Osama Mersal
WPML Supporter since 02/2020

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

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

Hi,

Thanks for contacting WPML forums support. I'll be glad to help you today.

Please log in to this sandbox site and create a custom post status to replicate the issue. (hidden link)

If the issue is replicable, please let me know how to reproduce it so I can check it and consult our 2nd iter support team if needed.

Best regards,
Osama

May 14, 2024 at 3:00 am #15623072

Paradigm

I have uploaded my theme and setup the scenario.
This "noo_company" custom post will show hreflang tags if the post status is "Publish" however is will not if the post_status is "dormant".

hidden link

I want the hreflang tags to show because the page is public.

I have checked the WPML code and I am fairly sure this behaviour will be present in any theme.

May 14, 2024 at 7:58 am #15623752

Osama Mersal
WPML Supporter since 02/2020

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

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

Hi,

Thanks for replicating the issue. I've consulted our 2nd tier support team regarding this issue and will update you as soon as I get their reply.

Best regards,
Osama

May 15, 2024 at 12:44 pm #15630471

Osama Mersal
WPML Supporter since 02/2020

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

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

Hi,

Thanks for your patience. Please add the following code to your functions.php.

/**
 * Fix post status issue for WPML hreflangs.
 * 
 * @link wpmlsupp-11719
 */
add_filter('wpml_head_langs', function($args){
	$change_post_status = function( $post_status ) {
		//Change dormant to actual custom post status.
		if ($post_status === 'dormant') {
			return 'publish';
		}

		return $post_status;
	};

	add_filter('get_post_status', $change_post_status);

	add_filter('wpml_hreflangs', function($args) use ($change_post_status){
		remove_filter('get_post_status', $change_post_status);
		return $args;
	});

	return $args;
});

After that, please check the hreflangs for the posts. (view-source:hidden link)

Best regards,
Osama

May 16, 2024 at 4:38 am #15632795

Paradigm

Thank you, that worked perfectly.