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 thread is resolved. Here is a description of the problem and solution.

Problem: How to redefine standard posts as custom post types in database ?

Solution: You have 2 steps to follow

#1 Change the post type definition in wp_post table (this is not related to WPML). In this example, let's say the new post_type id is 'custom_news'.

UPDATE wp_posts SET post_type = 'custom_news' WHERE post_type = 'post';

#2 Change the element type definition in wp_icl_translations table (this is related to WPML).

UPDATE wp_icl_translations SET element_type = 'post_custom_news' WHERE element_type = 'post_post';

Do not forget to change the table prefix and the custom post type id in each query

This topic contains 14 replies, has 2 voices.

Last updated by pamelaB 4 years, 5 months ago.

Assigned support staff: Pierre.

Author Posts
April 9, 2015 at 4:20 pm #596763

pamelaB

Just to follow up here: https://wpml.org/forums/topic/updated-post-to-custom-post-type-and-how-do-i-update-their-translations/
As Beda suggested, the custom post type I created is missing from the 'Translatable custom posts linking' list, and can you advise what should I do next please?
Thanks,
Fan

April 9, 2015 at 4:22 pm #596765

pamelaB

I closed the other support request by mistake.

And here are the screenshots - 'News' is the custom post type I created.

April 10, 2015 at 6:48 pm #597511

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

In the "Translatable custom posts linking", you should have a dropdown in front of your new custom post type. Can you select it?

If this does not solve the problem, can you click on "Fix post type assignement for translations"?

Let me know it this works.

Regards,
Pierre

April 13, 2015 at 10:57 am #598232

pamelaB

Hi Pierre

My custom post type (custom_news) doesn't appear in the 'Translatable custom posts linking' list unfortunately however all my other custom post types (e.g. custom_application, custom_career, custom_product) are all in the list.

When I click "Fix post type assignment for translations" it says 'No errors were found in the assignment of post types.'

Thanks,
Fan

April 13, 2015 at 4:43 pm #598523

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Fan,

How did you created your custom post type?
If you created it with code, can you share the code here?

Regards,
Pierre

April 17, 2015 at 4:17 pm #601622

pamelaB

Hi Pierre

Sorry for the late response. As previous said, all the other custom post types I created and used all appear on the list apart from the 'custom_news' type.

They all use the very similar codes below:

<?php
global $custom_wp_post_types;
if(($custom_wp_post_types) && (is_array($custom_wp_post_types)))
{
    $custom_wp_post_types[] = 'custom_news';
}
else
{
    $custom_wp_post_types = array('custom_news');
}

//custom_news post type
function create_custom_news_post_type()
{
    //register post type
    register_post_type('custom_news',
        array(
            'labels' => array(
               'name' => 'News',
               'singular_name' => 'News',
               'add_new' => 'Add New',
               'add_new_item' => 'Add New News',
               'edit_item' => 'Edit News',
               'new_item' => 'New News',
               'all_items' => 'All News',
               'view_item' => 'View News',
               'search_items' => 'Search News',
               'not_found' =>  'No News found',
               'not_found_in_trash' => 'No News found in Trash',
               'parent_item_colon' => '',
               'menu_name' => 'News'
            ),
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'has_archive' => false,
            'hierarchical' => true,
            'rewrite' => array(
               'slug' => 'news',
               'with_front' => false
            ),
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'page-attributes', 'revisions')
        )
    );
}
add_action('init', 'create_custom_news_post_type');

function custom_news_pagination_rule()
{
	global $wp;
	add_rewrite_rule('news/page/([0-9]{1,})/?$','index.php?pagename=news&paged=$1','top');
	
	 //only flush when not live
	if(get_option('general_cms_force_hide_from_google') != 1) {
		global $wp_rewrite;
		$wp_rewrite->flush_rules();
	}
    
}
add_action('init', 'custom_news_pagination_rule');

function custom_news_messages($messages)
{
    global $post, $post_ID;

    $messages['custom_news'] = array(
        0 => '', // Unused. Messages start at index 1.
        1 => sprintf(__('News updated. <a href="%s">View News</a>'), esc_url(get_permalink($post_ID))),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('News updated.'),
        /* translators: %s: date and time of the revision */
        5 => isset($_GET['revision']) ? sprintf(__('News restored to revision from %s'), wp_post_revision_title((int)$_GET['revision'], false)) : false,
        6 => sprintf(__('News published. <a href="%s">View News</a>'), esc_url(get_permalink($post_ID))),
        7 => __('News saved.'),
        8 => sprintf(__('News submitted. <a target="_blank" href="%s">Preview News</a>'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))),
        9 => sprintf(__('News scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview News</a>'),
            // translators: Publish box date format, see <em><u>hidden link</u></em>
            date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
        10 => sprintf(__('News draft updated. <a target="_blank" href="%s">Preview News</a>'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))),
    );
    return $messages;
}
add_filter('post_updated_messages', 'custom_news_messages');
?>

Many thanks,
Fan

April 17, 2015 at 6:37 pm #601688

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi Fan,

Thanks for your reply.

Well, I don't see any thing wrong with your post registration.

Now, I am not quite sure to understand your problem. As far as I can see in your screenshots, you have the same total amount of posts. The only difference is the language information which may be corrupted.

Basically, if you check the icl_translations table, you will see a record for each post. In the record, you'll find information about the language and the source language. To find the corresponding translation to a post, search records with the same trid.

Note that the source post should have 'source_language_code' set to NULL.

Can you have a look at this table and see if everything is correct?

Regards,
Pierre

April 20, 2015 at 9:10 am #602297

pamelaB

Hi Pierre

I just attach two more screenshots - looks the posts are fine now (oddly it sorted itself out) but the news are still wrong - there is no news at all but it shows English (97).

I have done the following query:
SELECT * FROM wp_icl_translations WHERE element_type = 'post_custom_news'
but it finds nothing - none of the entries are in 'post_custom_news' element_type.

So... do you know where the number 97 comes from in the DB? maybe track down from this clue (97)?

Thanks,
Fan

April 20, 2015 at 2:53 pm #602608

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Fan,

To make sure because I didn't ask, are you using the default table prefix (wp_)?

If yes, this means the language information is still on the previous post type.
So, you should be able to change it by changing "post_post" to "post_custom_news" in this table.

Can you try this?

Please, take a full database backup previously.

Regards,
Pierre

April 20, 2015 at 3:15 pm #602621

pamelaB

No, actually not, sorry, I just didn't write the full table name above (wp_af_icl_translations).

When I run:
SELECT * FROM wp_af_icl_translations WHERE element_type = 'post_post';
It does list 97 records which match up with the reply #602297.

April 20, 2015 at 5:09 pm #602714

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

I need to request temporary access (wp-admin and FTP) to your site - preferably to a test site where the problem has been replicated if possible - in order to be of better help.

You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Please take a full database backup.

Regards,
Pierre

April 22, 2015 at 3:42 pm #604203

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello Fan,

There were 2 problems:

1. In wp_post table, the news records where still having "post" as post_type.
So I applied this query:

UPDATE wp_af_posts SET post_type = 'custom_news' WHERE post_type = 'post';

2. In icl_translations, the records with "post_post" as element_type should be updated to "post_custom_post".
So I applied this query:

UPDATE wp_af_icl_translations SET element_type = 'post_custom_news' WHERE element_type = 'post_post';

Can you confirm this matches your requirements?

Regards,
Pierre

April 22, 2015 at 4:08 pm #604233

pamelaB

Ah! So I assume that was caused by me updating the post type in one table but not the other?
I will pay more attention to this.
Thanks for looking into this for me.

Just have another issue unfortunately, I think it's related to WPML but not 100% certain - we have been creating several Contact Form 7 forms from: /wp-admin/admin.php?page=wpcf7 but none of them appear on the list now since post_id = 2227 (about 1-2 hours ago). But as I can see, all the forms are actually created in the wp_af_posts table! However, when I check the wp_af_icl_translations table, it only counts 8 post_wpcf7_contact_form element_type records created previously (about 1 - 2 hours ago).

Any idea?

Many thanks again!
Fan

April 22, 2015 at 4:27 pm #604247

Pierre
Supporter

Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Fan,

Glad to hear you are happy with this solution. As the original issue is resolved may I kindly ask you please to open a new ticket for your new question? This will also help other users with similar problems find solutions when searching the forum.

Regards,
Pierre

April 22, 2015 at 4:36 pm #604255

pamelaB

Ok will do. Many thanks Pierre for all your help 🙂