A new multilingual post (2 languages, de/en) is to be created in a frontend form.
WPML is used throughout the site for multilingualism.
A new post in one language can easily be created with ACF (ACF pro) frontend form-
However, the new post should always be created in both languages (from the frontend) at the same time.
How can I achieve this?
Solution:
[code]
function duplicate_post_to_secondary_language( $post_id, $mark_as_duplicate = false ) {
// Retrieve the post's element type, trid, and existing translations
$element_type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
$trid = apply_filters( 'wpml_element_trid', null, $post_id, $element_type );
$translations = (array) apply_filters( 'wpml_get_element_translations', [], $trid, $element_type );
if ( ! $translations ) {
// Handle error or invalid values accordingly
return;
}
// Retrieve the active languages
$wpml_languages = (array) apply_filters('wpml_active_languages', [], 'orderby=id&order=desc');
foreach ( $wpml_languages as $language ) {
// Duplicate the post if no translation exists for the given language
if ( ! isset( $translations[ $language['language_code'] ] ) ) {
apply_filters(
'wpml_copy_post_to_language',
$post_id,
$language['language_code'],
$mark_as_duplicate
);
}
}
}
add_action('acf/save_post', 'duplicate_post_to_secondary_language', 20);