This thread is resolved. Here is a description of the problem and solution.
Problem:
The client needs to automatically create a duplicate of each Spanish (Spain) page/post for Spanish (Mexico) on their multilingual site, but only for certain pages.
Solution:
We recommend using the WPML hook for duplicating posts automatically. First, ensure you back up your site and database. You can implement the following code in your theme's functions.php file or a custom plugin:
add_action( 'wp_insert_post', 'my_duplicate_on_publish' ); function my_duplicate_on_publish( $post_id ) { global $post; if (is_null($post) || (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) || (isset( $post->post_type ) && $post->post_type == 'revision')) { return $post_id; } remove_action( 'wp_insert_post', 'my_duplicate_on_publish' ); $is_translated_in_es_mx = apply_filters( 'wpml_element_has_translation', '', $post_id, 'es-mx' ); if ( !$is_translated_in_es_mx ) { do_action( 'wpml_admin_make_post_duplicates', $post_id ); do_action( 'wpml_set_element_language_details', [ 'element_id' => $post_id, 'element_type' => 'post_' . $post->post_type, 'trid' => wpml_get_content_trid( 'post_' . $post->post_type, $post_id ), 'language_code' => 'es-mx', 'source_language_code' => 'es' ] ); } add_action( 'wp_insert_post', 'my_duplicate_on_publish' ); return $post_id; }
This script checks if a post is a new or updated Spanish post and duplicates it for Spanish (Mexico) if no translation exists. Modify the condition inside the function if you want to limit this behavior to specific post types.
For more details on the hook used, visit WPML admin make post duplicates.
If this solution does not apply to your case, or if it seems outdated, please check the related known issues, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. If further assistance is needed, we encourage you to 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.
This topic contains 0 replies, has 1 voice.
Last updated by 1 week, 5 days ago.
Assisted by: Andreas W..