Skip Navigation

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.

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Bruno Kos 9 months, 2 weeks ago.

Assisted by: Bruno Kos.

Author Posts
February 5, 2024 at 4:37 pm #15267392

panagiotisE

Hi,

I found the code below for what I need and works fine. But I need to be applicable only to custom post type "event".

add_filter( 'icl_job_elements', 'filter_title', 10, 2 );
function filter_title( $fields, $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;
}

February 6, 2024 at 7:17 am #15269091

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+01: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".

Regards,
Bruno Kos

February 6, 2024 at 8:20 am #15269278

panagiotisE

It works perfect! Thank you!

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.

if ( $value->field_type == "title" || $value->field_type == "url" || $value->field_type == "slug" )

February 6, 2024 at 9:13 am #15269752

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+01: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.

February 6, 2024 at 10:49 am #15270717

panagiotisE

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.

February 6, 2024 at 2:52 pm #15272445

Bruno Kos
Supporter

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+01: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.

https://wpml.org/forums/topic/keep-slugs-in-english/#post-13467257

encode.jpg