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 6 replies, has 3 voices.
Last updated by xavierc-4 6 years ago.
Assigned support staff: Iris.
Author | Posts |
---|---|
January 6, 2015 at 2:32 pm #537861 | |
janekS |
I am trying to: create programmatically post in 2 languages with different titles. How can i do it with wpml-api.php? Step 1: wp_insert_post //creates post in default language and i can see it in table "icl_translations" Issue is related to hidden link and wpml-api.php. $post_id = wp_insert_post( array( 'post_title' => 'Test post in russian -' . time(), 'post_status' => 'publish' ) ); $_POST['icl_post_language'] = $language_code = 'ru'; wpml_add_translatable_content( 'post_post', $post_id , $language_code ); As suggested i used wpml_add_translatable_content but it did not work and return 7(WPML_API_CONTENT_EXISTS) because element_id with such integer is already in this table(after we used wp_insert_post) I tried wpml_update_translatable_content function(as post_id i inserted random number) it works fine, i can see that new post is created in another language, but it does not connect to post, which is created in default language. Can you help me to understand how wpml_add_translatable_content and wpml_update_translatable_content work, what parameters i have to use and what for i can use this function for? |
January 6, 2015 at 9:33 pm #538087 | |
Iris |
Hello, I need a little more clarification on what you're trying to achieve, and what you mean by programmatically post in two languages. Are you trying to import a large number of contents that need translation? Could you tell me in little more about the file type you're importing or the platform you're importing from? I noticed that the information in the link you provided was from 2012. That we have had updates since then. If it is the case that you are trying to import contents, there are ways to achieve this. The following links might be able to assist you: I also noticed that your WordPress needs to be updated. We recommend that you back up your site before doing so. If any plugins need to be updated, please make sure to update those as well. Could you please clarify the error you're having so that I can better assist you? Thanks. |
January 7, 2015 at 8:40 am #538222 | |
janekS |
My idea is that user fills in form from front-end. Form contains the following fields: |
January 7, 2015 at 12:23 pm #538434 | |
janekS |
I resolved my issue. Could you tell me, is there better solution for that issue? function multilingual_post_creation(){ global $sitepress; /** /** Creating first post in english */ //arguments for post creation $def_args = array( 'post_title' => '[en]Test post - ' . date("d.m - H:i:s"), 'post_content' => 'Text in default language goes here', 'post_status' => 'publish', 'post_type' => 'post' ); //creating post with arguments above and assign post id to $def_post_id $def_post_id = wp_insert_post($def_args); $def_trid = $sitepress->get_element_trid($def_post_id); /** Creating translation of first post in another language. Right now for russian */ //arguments for post creation $ru_args = array( 'post_title' => '[ru]Test post - ' . date("d.m - H:i:s"), 'post_content' => 'Text in russian language goes here', 'post_status' => 'publish', 'post_type' => 'post' ); //creating post with arguments above and assign post id to $ru_post_id $ru_post_id = wp_insert_post($ru_args); //change language and trid of second post to match russian and default post trid $sitepress->set_element_language_details($ru_post_id, 'post_post', $def_trid, 'ru'); } |
January 7, 2015 at 5:24 pm #538663 | |
Iris |
Hello, Thank you for sharing the details. To learn more about WPML's tables, please see the section titled Viewing WPML's tables at this link: http://wpml.org/documentation/support/wpml-tables/ Thanks. |
January 8, 2015 at 8:12 am #538963 | |
janekS |
Issue resolved |
November 6, 2018 at 4:05 pm #2881733 | |
xavierc-4 |
Hi! JanekS solution has been the best for me: //change language and trid of second post to match secondary language and default post trid (default language) $sitepress->set_element_language_details($ru_post_id, 'post_post', $def_trid, 'ru'); In my case I have connected two posts with the post type (relojes): global $sitepress; $sitepress->set_element_language_details($insertedId, 'post_relojes', $originalPost->ID, $lang); The documented WPML option to do the same thing it's not well documented and it seems difficult to use because of some wierd parameters... https://wpml.org/wpml-hook/wpml_set_element_language_details/ |