Skip Navigation
availability:

WPML Version: 3.2

description:

Builds or updates duplicate posts from a “master” post. This action hook is reserved for admin backend calls.

type:
action
category:
Inserting Content
parameters:
do_action( 'wpml_admin_make_post_duplicates', int $master_post_id )
$master_post_id
(int) (Required) The ID of the post to duplicate from. It can be that of a post, page or custom post. The “master post” doesn’t need to be in the default language.
hook example usage:

Example

1
2
// this will create duplicates for the "Hello World!" post
do_action( 'wpml_admin_make_post_duplicates', 1 );

In the example below, we create duplicates for a post or page or custom post when we click on “publish” from the “edit-post” admin screen.

A more advanced example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;
 
    // don't save for autosave
    if (is_null($post))
        {
             return $post_id;
        }
 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
 
    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
 
    // we need this to avoid recursion see add_action at the end
    remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
 
    // make duplicates if the post being saved:
    // #1. is not a duplicate of another or
    // #2. does not already have translations
 
    $is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
 
    if ( !$is_translated ) {
        do_action( 'wpml_admin_make_post_duplicates', $post_id );
    }
 
    // must hook again - see remove_action further up
    add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
}