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.

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

This topic contains 3 replies, has 2 voices.

Last updated by Andreas W. 3 months ago.

Assigned support staff: Andreas W..

Author Posts
July 15, 2019 at 12:05 pm #4210831

robertF-26

Hi,

I’m using the following code to generate translations/duplicates of my custom post
types.
This works for the posts but the post meta data from the original will not be copied
or updated any more. Are there any modifications in (using) "wpml_admin_make_post_duplicates"?
Thanks!

<?php

function my_make_duplicate( $master_post_id, $lang ) {
    global $sitepress;
    if ($lang != 'en') {
               return $sitepress->make_duplicate( $master_post_id, $lang );
    }
}

function my_make_duplicates_all( $master_post_id ) {
        global $sitepress;

        $master_post = get_post( $master_post_id );
        if ( $master_post->post_status == 'auto-draft' || $master_post->post_type == 'revision' ) {
            return;
        }

        $language_details_original = $sitepress->get_element_language_details( $master_post_id, 'post_' . $master_post->post_type );

        if ( ! $language_details_original ) {
            return;
        }

        $data[ 'iclpost' ] = array( $master_post_id );
        foreach ( $sitepress->get_active_languages() as $lang => $details ) {
             
            if ( $lang != $language_details_original->language_code) {
                $data[ 'duplicate_to' ][ $lang ] = 1;
            }
             
        }
               my_make_duplicate( $master_post_id, 'ch' );
        //my_make_duplicates( $data );
    }

function my_wpml_admin_make_post_duplicates_action( $master_post_id ) {
    $post      = get_post( $master_post_id );
    $post_type = $post->post_type;

    if ( $post->post_status == 'auto-draft' || $post->post_type == 'revision' ) {
        return;
    }

    global $sitepress;
    $iclTranslationManagement = wpml_load_core_tm();
    if ( $sitepress->is_translated_post_type( $post_type ) ) {
        my_make_duplicates_all( $master_post_id );
    }
}
add_action( 'wpml_admin_make_post_duplicates', 'my_wpml_admin_make_post_duplicates_action', 10, 1 );

add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;
    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
        // don't save if not project
        if ( isset( $post->post_type ) && $post->post_type != "projekt" ) {
               return $post_id;
        }
        // don't save if language not de
        if ( apply_filters( 'wpml_post_language_details', NULL, $post_id )["language_code"] != "de" ){
               return $post_id;
        }
        // don't save if translation already exists

    // we need this to avoid recursion see add_action at the end
    remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
    remove_action( 'wpml_admin_make_post_duplicates', 'wpml_admin_make_post_duplicates_action', 10, 1 );
  
        // make duplicates if the post being saved
        // #1. itself is not a duplicate of another or
        // #2. does not already have translations
  
        $is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
  
        if ( !$is_translated ) {
            do_action( 'wpml_admin_make_post_duplicates', $post_id );
        }
  
    // must hook again - see remove_action further up
    add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
}
?>
July 15, 2019 at 5:01 pm #4213663

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

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

Hello,

Thank you for contacting the WPML Support Forum.

We have tested the hook for wpml_admin_makes_post_duplicates and it works as expected.

Could you please try to use this code instead:

add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;
    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
 
    // we need this to avoid recursion see add_action at the end
    remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
 
        // make duplicates if the post being saved
        // #1. itself is not a duplicate of another or
        // #2. does not already have translations
 
        $is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
 
        if ( !$is_translated ) {
            do_action( 'wpml_admin_make_post_duplicates', $post_id );
        }
 
    // must hook again - see remove_action further up
    add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
}

Code Reference:

https://wpml.org/wpml-hook/wpml_admin_make_post_duplicates/

Let me know if you need any further assistance.

Kind regards
Andreas

July 16, 2019 at 7:44 am #4217203

robertF-26

Hello Andreas,

thanks for replying.
I need these contraints because the original post should only be dublicated when it is of type
'projekt' and I only need dublicates of the german speaking posts.

        // don't save if not project
        if ( isset( $post->post_type ) && $post->post_type != "projekt" ) {
               return $post_id;
        }
        // don't save if language not de
        if ( apply_filters( 'wpml_post_language_details', NULL, $post_id )["language_code"] != "de" ){
               return $post_id;

But I removed the following:

remove_action( 'wpml_admin_make_post_duplicates', 'wpml_admin_make_post_duplicates_action', 10, 1 );

There was no effect. For testing I generated a new test post with meta datas but these wouldn't be dublicated / translated (see attachment pic):
Thanks for your support.

Regards

Markus

July 16, 2019 at 5:49 pm #4223061

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

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

Hello,

I am really sorry for not being able to assist you further with your request but please take note that our support policy does not cover any custom requests.

More information can be found here:
https://wpml.org/purchase/support-policy/

I would advise you to reach out to one of our contractors. Here you will find expereicned developers with knowledge of WordPress and WPML.
https://wpml.org/contractors/

We hope for your understanding.

Kind regards
Andreas