[Resolved] Remove title from translation only for custom post type
This thread is resolved. Here is a description of the problem and solution.
Problem: The client needed to apply a PHP filter specifically to the custom post type "event".
Solution: We recommended modifying the existing function to check the post type before applying the filter. Here's the step-by-step solution:
add_filter( 'icl_job_elements', 'filter_title_for_events_only', 10, 2 );<br />function filter_title_for_events_only( $fields, $post_id ) {<br /> // Check if the post type is 'event'<br /> if ( 'event' === get_post_type( $post_id ) ) {<br /> foreach ( $fields as $field => $value ) {<br /> if ( $value->field_type == "title" ) {<br /> $value->field_translate = 0;<br /> $value->field_data_translated = $value->field_data;<br /> }<br /> }<br /> }<br /> return $fields;<br />}
This code snippet should be added to the theme's functions.php file or a custom plugin. It ensures that the filter is only applied to the title fields of posts with the custom post type "event".
Please note that this solution might be outdated or not applicable to your case. If it doesn't solve your issue, we highly recommend checking the related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If you still need assistance, 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.
Languages: English (English )German (Deutsch )French (Français )
Timezone: Europe/Zagreb (GMT+02:00)
Hi,
Thank you for contacting WPML support!
To make the code applicable only to a custom post type "event", you need to check the post type of the post being processed before applying the changes. This can be achieved by getting the post type of the `$post_id` and then applying your modifications only if the post type is "event". You can use `get_post_type()` function for this purpose.
Here's how you can modify your function - I haven't tested this though.
add_filter( 'icl_job_elements', 'filter_title_for_events_only', 10, 2 );
function filter_title_for_events_only( $fields, $post_id ) {
// Check if the post type is 'event'
if ( 'event' === get_post_type( $post_id ) ) {
foreach ( $fields as $field => $value ) {
if ( $value->field_type == "title" ) {
$value->field_translate = 0;
$value->field_data_translated = $value->field_data;
}
}
}
return $fields;
}
This modified version of your function first checks if the post type of the current post (`$post_id`) is `event`. If it is, it proceeds with the loop to apply your modifications to the title field. If the post type is not `event`, it simply returns the fields without any changes. This way, your modifications are applied only to posts of type "event".
One last question... "event" post type is used just for data entry, it has not public url. So is it possible to remove slug too, as the title? I tried the below but it doesn't work.
Languages: English (English )German (Deutsch )French (Français )
Timezone: Europe/Zagreb (GMT+02:00)
To remove slugs from where, translation editor? Because by default it will not appear there.
In WordPress, you cannot disable slugs entirely for post types because slugs are a fundamental part of how WordPress handles permalinks and URL structure for posts, pages, and custom post types.
I need to have the same url in all languages for an other custom post type. For this reason in settings at Page URL I selected "Translate (this will include the slug in the translation and not create it automatically from the title)". Maybe for this reason slug appears in translation editor.
Languages: English (English )German (Deutsch )French (Français )
Timezone: Europe/Zagreb (GMT+02:00)
There is no such feature (even though it is being considered), but you can consider setting that language so that it uses encoded URLs, even though it doesn't.
Because if you set "2. Copy from original language if translation language uses encoded URLs", it would copy the slugs then.