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 5 replies, has 2 voices.

Last updated by Raja Mohammed 6 months, 3 weeks ago.

Assigned support staff: Raja Mohammed.

Author Posts
March 26, 2019 at 9:52 pm #3466639

charlottev-6

Hi,

When importing a xliff file, I want to generate some content for a custom field, based on the values in the xliff file.

Is there a way to get the post_id of the new post created when importing a xliff file.

It is clear for me that when importing a xliff file, you can update the custom field, via a hook "add_action('save_post".....", see example:

function example_do_on_save(){
	
	$post_id   = HOW DO I GET THE POST_ID?;
	$post_type = get_post_type($post_id);
	
	$current_language = apply_filters( 'wpml_current_language', NULL );
	
	// do only something when Post Type is event
	if ($post_type == 'event'){
		.......
	}
}
add_action( 'save_post', 'example_do_on_save' );

But how can I get the post ID during importing?

Maybe this is the good direction:
https://wpml.org/wpml-hook/wpml_tm_save_translation_cf/
But is is not clear how this filter works?

March 27, 2019 at 7:21 am #3468175

charlottev-6

Hi,

I figured out, when importing a xliff file, for a translation of a post of which not yet a page_id is available the following $_POST is given during import:

array (
  'xliff_upload' => 'Upload',
  'trid' => '640',
  'lang' => 'nl',
  'skip_sitepress_actions' => true,
  'needs_second_update' => 0,
)

So the TRID is available, but not yet the new post_id.

March 27, 2019 at 9:01 am #3468823

charlottev-6

Hi,

I think I have the solution.

I have to use:

add_action( 'wp_insert_post', 'example_do_on_save' );

The post_id is available using this hook.

March 27, 2019 at 10:46 am #3469813

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

HI there,

Welcome to the forum,

Seems you have the solution already, The hook "wp_insert_post" will give you access to the $post array through which you can get the post id.

Let me know if you need further assistance.

Kind regards
Raja

March 27, 2019 at 11:58 am #3470657

charlottev-6

mmm,
unfortunately this is not the solution. OK I can get the page_id, but the custom fields (imported from translation, and copied by WPML are not yet available via e.g.:

get_post_meta($post_id,'some_test_country',true); 

so, I think I need a hook that is fired directly after "importing" the xliff file on the moment all new values are in the database....

March 27, 2019 at 12:49 pm #3471205

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

You have mentioned in your second response you managed to get the TRID, Then using TRID you can get the post details using the filter "wpml_get_element_translations"

Please refer https://wpml.org/wpml-hook/wpml_get_element_translations/

The element_id in the returned array is the post_id.

I hope this helps you achieve your desired results

Kind regards
Raja