我正在使用WPUF Pro 提交自定义帖子类型的表单vehicle
, 通过前端。我添加了一个自定义函数来在发布时自动复制帖子。
我注意到,当我收到第二语言(非默认)的表单并提交我的车辆时,该功能不会为默认语言创建重复帖子,即使相反的语言运行良好。
function wpml_duplicate_on_publish($post_id)
{
global $post, $sitepress, $iclTranslationManagement;
// don\'t save for autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
// save only for campaign
if (isset($post->post_type) && $post->post_type != \'vehicle\') {
return $post_id;
}
// get languages
$langs = $sitepress->get_active_languages();
unset($langs[$sitepress->get_default_language()]);
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', \'wpml_translate_post\');
// make duplicates if the post being saved does not have any already or is not a duplicate of another
$has_duplicates = $iclTranslationManagement->get_duplicates($post_id);
$is_duplicate = get_post_meta($post_id, \'_icl_lang_duplicate_of\', true);
if (!$is_duplicate && !$has_duplicates) {
//now lets create duplicates of all new posts in all languages used for translations
foreach ($langs as $language_code => $v) {
$iclTranslationManagement->make_duplicate($post_id, $language_code);
}
}
}
add_action(\'wpuf_add_post_after_insert\', \'wpml_duplicate_on_publish\');
有什么建议吗?提前谢谢你。
最合适的回答,由SO网友:cybmeta 整理而成
问题是您正在取消设置默认语言表单$langs
数组,所以当你循环$langs
要制作副本,从不包括默认语言。这可能是您问题的原因(顺便说一下,与WordPress无关)。
function wpml_duplicate_on_publish($post_id) {
global $post, $sitepress, $iclTranslationManagement;
// don\'t save for autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
// save only for campaign
if (isset($post->post_type) && $post->post_type != \'vehicle\') {
return $post_id;
}
// get languages
$langs = $sitepress->get_active_languages();
// Unseeting the default lenguage was the problem
// unset($langs[$sitepress->get_default_language()]);
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', \'wpml_translate_post\');
// make duplicates if the post being saved does not have any already or is not a duplicate of another
$has_duplicates = $iclTranslationManagement->get_duplicates($post_id);
$is_duplicate = get_post_meta($post_id, \'_icl_lang_duplicate_of\', true);
if (!$is_duplicate && !$has_duplicates) {
//now lets create duplicates of all new posts in all languages used for translations
foreach ($langs as $language_code => $v) {
$iclTranslationManagement->make_duplicate($post_id, $language_code);
}
}
}
add_action(\'wpuf_add_post_after_insert\', \'wpml_duplicate_on_publish\');