3

I wan't to achieve following: I have to save a duplicate of a newly created post if a user creates a new post (cpt). Afterwards I will set this new post with Polylang to another language as translation for the post created in step one.

Hook into following actions should lead to the desired result:

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');
    }

}

But unfortunately this will result in an infinite loop (as expected). Now I'm looking for a possibility to avoid this loop. My first idea was to set a $_POST variable and only execute duplication if this variable is set. But I have no idea how to identify a new post. I discovered that WordPress immediately saves an auto-draft on clicking the 'New Post' button so looking for post ID = 0 doesn't work. Any other approach is highly welcome.

Dhruvin Moradiya
  • 546
  • 2
  • 10
  • 20
Ole_S
  • 356
  • 1
  • 3
  • 21

1 Answers1

1

For everyone who needs a solution:

I use the method called to save the meta data to accomplish duplicating my post:

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;
}

That's it. Now everytime you create a new post or update an existing post a translated duplicate will be created or updated.

Ole_S
  • 356
  • 1
  • 3
  • 21