Skip Navigation

[Resolved] Toolset CPT with custom repeatable fields and WPML translation issues

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

Problem:

The issue here is that the user wanted to get the language of a specific post as well as the Language of the current page on the frontend.

Solution:

This can be done by using the shortcodes below.


// Add Shortcode
function get_post_language( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
        ),
        $atts
    );
 
    $language = apply_filters( 'wpml_post_language_details', NULL, $atts['id'] ) ;
     
    return $language['language_code'];
 
}
add_shortcode( 'get_post_language', 'get_post_language' );

To get the current language being viewed you can use the shortcode below.

// Add Shortcode
function current_language() {
 
     $my_current_lang = apply_filters( 'wpml_current_language', NULL );
    return $my_current_lang;
 
}
add_shortcode( 'current_language', 'current_language' );

You can add them to the toolset custom code section in Toolset -> Settings -> Custom Code and activate the snippet.

The shortcodes that will be generated are
[get_post_language id='[wpv-post-id]']

And
[current_laguage]

Now for the get_post_language shortcode i'm passing in the id of a post to get the language of that post based on its ID.

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.69 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Andreas K. 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1325909
toolset_repeatablefields_issue.png

Hi

I am using Toolset for a CPT on a site along with WPML with two languages and Toolset Views with a search function to show the CPT posts as results.

The CPT has custom fields and some of them are repeatable fields.

And this is where my problem is.

As I have two languages and the CPT posts are going to be written in language 1 first and published and then slowly translated one by one into language 2, I need the views/search to only show the CPT posts for language 1, when that language is selected on the language selector in the menu, and only show the CPT posts for language 2, when that one is selected.

For this I would select “Translatable - only show translated items” for the CPT in the WPML settings, and not “Translatable - use translation if available or fallback to default language”.

But this cannot be done when using repeatable fields?

I am told in the Toolset repeatable customs fields when editing one of the CPT posts, that I need to enable the “Translatable - use translation if available or fallback to default language” setting to access/edit the field.

I seem to have read somewhere on either of your sites something about this also.

It is very unfortunate if this cannot be done as it makes the website work counterintuitive for the users/editors who do not wish to publish/show language 1 posts on language 2 as the fallback setting will it do.

I just switched from Polylang because of conflicts with Toolset Views, and really like WPML, but Polylang had no problem with separating the two language posts on the views though.

What to do? And why is this so?

The funny thing is, that when I do use the setting “Translatable - only show translated items” is does work on the frontend - only the selected language posts are show on the views - but I am just not allowed to edit the CPT fields in the backend, where I get the error (se the attached image).

If I have to use the “Translatable - use translation if available or fallback to default language”, there must be someway to hide the second language on the views, to make the views only show one/the active language??

Both Toolset and WPML is from the same company, as I understand, so I hoped, that they would work perfectly together…

Best regards,
Andreas

#1326327

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

Thank you for contacting our support forum.

Both Toolset and WPML is from the same company, as I understand, so I hoped, that they would work perfectly together

Yes we are the same company, however we constantly work together to try and improve this. Repeatable field groups is a relatively new functionality so constant improvements is being done here.

As I have two languages and the CPT posts are going to be written in language 1 first and published and then slowly translated one by one into language 2, I need the views/search to only show the CPT posts for language 1, when that language is selected on the language selector in the menu, and only show the CPT posts for language 2, when that one is selected.

I'm not sure I understand this statement here fully as this already happens with views. Views will list the posts relative to the language that was selected on your language selector.

For this I would select “Translatable - only show translated items” for the CPT in the WPML settings, and not “Translatable - use translation if available or fallback to default language

That is because repeatable field groups are relationships so if you want the relationships to remain they need to have a fallback in order to ensure that it is still connected to the original post. Take a look at the link below.
https://toolset.com/documentation/translating-sites-built-with-toolset/translating-repeatable-field-groups/

Thanks,
Shane

#1327043

HI Shane

Regarding:

As I have two languages and the CPT posts are going to be written in language 1 first and published and then slowly translated one by one into language 2, I need the views/search to only show the CPT posts for language 1, when that language is selected on the language selector in the menu, and only show the CPT posts for language 2, when that one is selected.

I'm not sure I understand this statement here fully as this already happens with views. Views will list the posts relative to the language that was selected on your language selector.

What I mean is, that when language 2 is selected/active and the website renders a view with posts and some of the posts are only in language 1 (not translated yet), then these post are shown in the view in the language of language 1. Which is confusing for both the editors of the website and the visitors...

I understand that you have some kind of problem regarding how you have setup the relations in the DB when using repeatable fields, but I cannot understand, that there isn't even some kind of work-around or views "hack" to hide the posts from the language which the views "falls back to" if they are not yet translated for the active language.

Can you confirm that there is no way to resolve this?

This must be a quit common problem for many people I would think - if your need the use both repeatable fields and WPML. Maybe you should prioritize it a bit higher or make it more clear, that these two options are actually not compatible. Using the fallback language is not a real solution for the users (devs and endusers), just at solution (or a patch) to a technical problem...

Sorry, but I am a disappointed in this - otherwise I really enjoy your plugins!

#1327427

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

If you are referring to the parent post then it should be displayed. However for the child because of the setting "Translatable - use translation if available or fallback to default language" Then it will fallback to the first language.

Only if the post language is set to "Translatable - only show translated items" then this is when views will display only the translated posts.

Can you confirm that there is no way to resolve this?

There actually is and you can hide the posts using this shortcode in combination with our conditional code.

add the following to the toolset custom code section in Toolset -> Settings -> Custom Code and activate the snippet.

// Add Shortcode
function get_post_language( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

	$language = apply_filters( 'wpml_post_language_details', NULL, $atts['id'] ) ;
	
	return $language['language_code'];

}
add_shortcode( 'get_post_language', 'get_post_language' );

What this shortcode does is to get the language code of the post based on the post ID. So how this works in a view is [get_post_language id='[wpv-post-id]'] where the [wpv-post-id] will retrieve the ID of the current post in the loop.

Then you can simply add it to a conditional code in views to let the view know that it should only display the posts of the current page language.

So here is an example.


[wpv-conditional if="( '[get_post_language id='[wpv-post-id]']' eq '[current_language]' )"]This is  a post[/wpv-conditional]

You will need to add this custom shortcode as well.


// Add Shortcode
function current_language() {

	 $my_current_lang = apply_filters( 'wpml_current_language', NULL );
	return $my_current_lang;

}
add_shortcode( 'current_language', 'current_language' );

This will get the language code of the currently selected language.

In order to use these shortcodes in our conditionals you will need to add them to the views 3rd party arguments in Toolset -> Settings -> Frontend -> 3rd Party shortcode arguments.

Please try this and let me know if this helps.

Thanks,
Shane

#1328493

Thank you so much, Shane!!

It works perfectly 🙂

Would you consider making it a part of Toolset somehow, maybe as a setting of some sort? Something one could enable for these special situations combing repeatable fields and WPML. Perhaps with a warning / information text when combining these two - and then with an option to enable this. It would be a nice way of handling the problem with WPML and repeatable fields - and no need to redo the database structure then 😉

You should definitely also link to this solution in your guides, docs etc...

Thanks once again.

Best,
Andreas

#1329701

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

I'm really Happy I could've assisted you with getting this one resolved.

Perhaps what you can do is to open a feature request for this so that it can be officially added to Toolset.

You can suggest your feature request at the link below.
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

This ticket can be searched through to get the solution once i've created a thread summary for it, so other customers can find it who are having similar issues.

You can go ahead and mark this one as resolved if there are no further issues.

Thanks,
Shane

#1330883

My issue is resolved now. Thank you!