Skip Navigation

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 2 voices.

Last updated by Andreas Neuber 7 years, 2 months ago.

Assigned support staff: Andreas Neuber.

Author Posts
August 1, 2014 at 7:57 am #445721



I currently added the following into function.php to create the custom post type:

// Create Listings Section
add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'listings',
      'labels' => array(
        'name' => __( 'Listings' ),
        'singular_name' => __( 'Listing' )
    'public' => true,
    'has_archive' => true,
	'rewrite' => array('slug' => 'listing'),


add_action('init', 'my_custom_init');
function my_custom_init() {
	add_post_type_support( 'listings', 'custom-fields' );
	add_post_type_support( 'listings', 'author' );
	add_post_type_support( 'listings', 'thumbnail' );
	add_post_type_support( 'listings', 'excerpt' );
	add_post_type_support( 'listings', 'comments' );

Afterwards I have a form in which I have created in the front page. Then within my custom form page I have created the following after the $_POST['action'] == "submit"

$uploadedfile = $_FILES['myfile'];
			$upload_overrides = array( 'test_form' => false );
			$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
			if ( $movefile ) {
				$wp_filetype = $movefile['type'];
				$filename = $movefile['file'];
				$wp_upload_dir = wp_upload_dir();
				$attachment = array(
					'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
					'post_mime_type' => $wp_filetype,
					'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
					'post_content' => '',
					'post_status' => 'inherit',
					'post_parent' => $post_id
				// Insert the attachment.
				$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );

				$get_attachment_info = $wpdb->get_results('SELECT * FROM `wp_posts` WHERE `ID` = "'.$attach_id.'"');
				// Generate the metadata for the attachment, and update the database record.
				$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
				$attach_id = media_handle_upload( 'myfile', $post_id );
				wp_update_attachment_metadata( $attach_id, $attach_data );
				set_post_thumbnail( $post_id, $attach_id );

I see the post is created but there are two problems. I cannot create the custom post in different language and how do I attach the image into the custom post and make it the feature post along with the different language?

Many thanks in advance!

August 1, 2014 at 10:47 am #445841

Andreas Neuber

Languages: English (English ) German (Deutsch )

Hello Timothy,

Looks interesting your code 🙂 thanks for posting, it helps us..

# "I cannot create the custom post in different language"
With WPML standard posts of type "post" and "page" are always "translatable".
But CPTs need to be set to being translatable.

In "WPML > Translation-Management (or options) > Custom posts" - do you see there the entry "Listing"?
If so is it set to translate?

# Media attachment
To be honest, in order to avoid that I tell you the wrong thing I will ask somebody in our team.

In general, translations are extra posts with different post IDs. --> icl_object_id() helps with getting the right ID

August 1, 2014 at 3:50 pm #446047


I was able to duplicate the post but I am still having trouble attaching the images to the custom post type.

August 4, 2014 at 7:23 am #446578

Andreas Neuber

Languages: English (English ) German (Deutsch )

Hi Timothy,

When WPML is not active does your code work then?

If so, what value does $post_id in line 23 return for every language (your 2nd code snippet, with active WPML)?

August 4, 2014 at 7:44 am #446591


Without WPML it works. I would see the media images are attached to the post in the media within posts.

Line 23 returns the post's id of the actual post which is returned by

$post_id = wp_insert_post( $post );

And the

$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );

The attach_id is the actual ID which is the attachment file. However, when I return back to the WordPress backend click on Media and look for the image that is attached to the post it is not there. Until I go to WPML -> Media Translation and click on "Start" and return to the post and I would see it there.

August 4, 2014 at 10:58 am #446763

Andreas Neuber

Languages: English (English ) German (Deutsch )

Hello Timothy,

Thanks for clarifying!
I will involve two of our developers for further feedback on this.

August 5, 2014 at 10:06 am #447463

Andreas Neuber

Languages: English (English ) German (Deutsch )

Hi Timothy,

My colleagues mentioned this:
- Attachments need to be duplicated as well and have language information set on them
This is basically what our "WPML-Media" plugin does. Therefore after you pressed the "Start" button there the attachments showed up.

# Could you also try this:

1) Create the 1st post in original language

2) Then duplicate the whole post with this code..

$post_id = [ ID of the newly created post];

function wpml_duplicate_on_publish ($post_id) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {

// No "Save" btn in backend involved..
//add_action('wp_insert_post', 'wpml_duplicate_on_publish', 10, 1);

wpml_duplicate_on_publish ($post_id); 

Since icl_make_duplicates() is one of our API functions, I think it will also take care of the attachments for you. Worth a try..

"$post->post_type" can be used to check the CPT

The topic ‘[Closed] Duplicating Post and Attaching Image to Custom Post Post’ is closed to new replies.