注意:我并没有给出您的问题的完整答案,但试图给出一些可能对基于WPML的项目有所帮助的片段。
我使用了此功能:
function get_the_translated_ID($id){
if (!class_exists(\'sitepress\')
return $id;
global $sitepress;
$type=get_post_type($id);
return icl_object_id( $id, $type, false, $sitepress->get_default_language());
}
例如,这允许搜索原始帖子的自定义\\u元值,而不是翻译。
循环中的示例:
$color = get_post_meta( get_the_translated_ID(get_the_ID() ), \'room_color\',true);
另一个有用的方法是,在用一种语言更新帖子时,在所有翻译中更新一些自定义元(这些元必须被视为独立于语言):
function bulk_CF_update($post_id){
if (!class_exists(\'sitepress\')
return;
$thisPost=get_post($post_id);
$allmeta=get_post_meta($post_id); // grab all custom meta fields of the post
$toUnset = array("CF_1","CF_2","CF_3"); // exclude the CF you want to keep \'per-language\'
foreach($toUnset as $unset)
unset($allmeta[$unset]);
if($thisPost->post_type=="roomtype"){ // do it only for one or more specific cpt
$trid = $sitepress->get_element_trid($thisPost->ID,\'post_roomtype\'); //note the prefix \'post_\' has to be added to your cpt slug
$translations = $sitepress->get_element_translations($trid);
foreach($translations as $translation){
foreach($allmeta as $meta=>$val){
if(count($val)==1) //we\'re only managing single values
update_post_meta($translation->element_id, $meta, $val[0]);
}
}
}
}
add_action( \'save_post\', \'bulk_CF_update\',10,1);
希望能有所帮助