php - WordPress 创建新帖子时保存翻译帖子

标签 php wordpress action addition polylang

我不想实现以下目标: 如果用户创建新帖子(cpt),我必须保存新创建帖子的副本。之后,我将使用 Polylang 将这个新帖子设置为另一种语言,作为第一步中创建的帖子的翻译。

Hook 以下操作应该会带来预期的结果:

add_action('new_to_publish', 'duplicate_to_english');
add_action('draft_to_publish', 'duplicate_to_english');
add_action('pending_to_publish', 'duplicate_to_english');

function duplicate_to_english($post)
{
    $en_post = pll_get_post($post->ID, 'en');
    if(empty($en_post)) {
        $new_post = (array) $post;
        unset($new_post['ID']);
        // INFINITE LOOP 
        $en_id = wp_insert_post($new_post);
        pll_set_post_language($en_id, 'en');
    }

}

但不幸的是,这将导致无限循环(如预期)。现在我正在寻找避免这种循环的可能性。我的第一个想法是设置一个 $_POST 变量,并且仅在设置了该变量时才执行复制。但我不知道如何识别新帖子。我发现 WordPress 在单击“新帖子”按钮后会立即保存自动草稿,因此查找帖子 ID = 0 不起作用。 任何其他方法都非常受欢迎。

最佳答案

对于每个需要解决方案的人:

我使用调用的方法来保存元数据来完成复制我的帖子:

add_action ( 'save_post', 'save_meta', 1, 3 ); // save the custom fields

function save_meta($post_id, $post, $update)
{
    // here we check for the transient set in duplicate_post
    // if existing delete it end return because we are saving only
    // the duplicate. This will avoid infinite loop while saving the
    // new translated post
    $english = get_transient('saving_english');
    if($english) {
        delete_transient('saving_english');
        return $post->ID;
    }

    // In case of auto saving draft we can return and don't duplicate the post
    if (!wp_verify_nonce($_POST['nonce'], 'nonce')) {
        return $post->ID;
    }

    if (! current_user_can ( 'edit_post', $post->ID )) {
        return $post->ID;
    }

    // collect meta data from $_POST
    $item_meta = $_POST['post_meta'];

    // Updating meta data for the original post
    loop_through_meta($item_meta, $post);

    // looking for translated version
    $en_id = pll_get_post($post->ID, 'en');

    // If the translated post is missing, set transient,
    // duplicate the post and category and afterwards write
    // the taxonomy entry for polylang
    if (empty($en_id)) {
        set_transient('saving_english', true);
        if ($en_id = duplicate_post($post, $item_meta['title_english'])) {
            pll_save_post_translations([
                'de' => $post->ID,
                'en' => $en_id
            ]);
        }
    // If the translated posts already exists deregister the hook
    // to avoid infinite loop.
    // But note the third parameter priority: It must be the same
    // priority as used for registering the hook
    } else {
        remove_action('save_post', 'save__meta',1);
        wp_update_post([
            'ID' => $en_id,
            'post_title' => $item_meta['title_english']
        ]);
        add_action ( 'save_post', 'save_meta', 1, 3 );
    }

    // If we have an id save new meta to the translated post
    if(!empty($en_id)) {
        loopt_through_meta($item_meta, get_post($en_id));
    }

}

function duplicate_post ($post, $title) 
{
    $new_post = (array) $post;
    unset($new_post['ID']);
    $new_post['post_title'] = $title;
    $new_id = wp_insert_post($new_post);

    // Here we only need to set the custom category but not
    // the Polylang taxonomy.
    $taxonomies = get_object_taxonomies($post->post_type); 
    foreach ($taxonomies as $taxonomy) {
        if($taxonomy != 'custom_categories') continue;
        $post_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'slugs'));
        wp_set_object_terms($new_id, $post_terms, $taxonomy, false);
    }

    pll_set_post_language($new_id, 'en');
    return $new_id;
}

就是这样。现在,每次您创建新帖子或更新现有帖子时,都会创建或更新翻译后的副本。

关于php - WordPress 创建新帖子时保存翻译帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020630/

相关文章:

c# - 如何动态地将条件和方法传递给递归方法

php - UNION表查询错误数据

php - memcached 与 PHP 中的内部缓存?

javascript - 从基于 WordPress 构建的 ReactJS 应用程序发出 POST 请求

c# - C# 中作为 Func 的操作

eclipse - 可以拦截eclipse中的“打开文件”菜单吗?

php - 您可以在 PHP 数组键中使用逗号吗?

php - 将类别树生成为 HTML 无序列表

javascript - Bootstrap - 响应式搜索按钮

php - 如何在 WordPress MySQL 查询中按元键值排序?