'How do I programatically create child posts, and assign to them the WPML information of their parent, so that they show in the WordPress admin?

I'm trying to create a set of child posts (of a custom post type names issues) when a new top-level post is created. The site is multilingual, and posts will (likely) only be created initially in the default language.

I do believe I have a flaw in my code's logic whereby some posts are being created erroneously without a parent, or with a child page as their parent, (perhaps due to translated posts triggering this function?) but for now my issue is that the programmatically created posts don't show in the admin, as they have no language information (when I go to WPML > Troubleshooting > Set language information the draft child posts then show).

From debug.log, it seems that $original_post_language isn't retrieving the parent post's language information, but I'm not sure why this would be.

Thank you so much for any help!

/*
    on publish of a new top-level issue, create a set of child issues
*/
function add_issue_child_posts($post_id) {
    // return if this is an autosave
    if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return;
    if (
        // check that this isn't a revision
        !wp_is_post_revision($post_id)
        // check that this post type is an issue
        && get_post_type($post_id) == "issues"
        // check that this post isn't an autosave
        && get_post_status($post_id) != "auto-draft"
    ) {
        $issue_being_saved = get_post($post_id);
        // check that this post isn't a child post
        if (isset($issue_being_saved->post_parent) && $issue_being_saved->post_parent == 0) {
            // check that this post isn't already a parent post
            $children =& get_children(
                array(
                    "post_parent" => $post_id,
                    "post_type"   => "issues",
                )
            );
            if (empty($children)) {
                // set generic arguments for all child posts we'll create this way
                $generic_child_page = array(
                    "post_type"    => "issues",
                    "post_title"   => "",
                    "post_content" => "",
                    "post_status"  => "draft",
                    "post_parent"  => $post_id
                );
                // set list of titles of child pages we'll create
                $child_page_titles = array(
                    "Definition & Legal Instruments",
                    "Contextual Risk Factors",
                    "Industry-specific Risk Factors",
                    "Due Dilligence Considerations",
                    "Case Studies",
                    "Further Guidance"
                );
                // reverse array to retain order of titles
                foreach ($child_page_titles as $child_page_title) {
                    // use generic child page arguments
                    $child_page_arguments = $generic_child_page;
                    // use current title from array as post title argument
                    $child_page_arguments["post_title"] = $child_page_title;
                    // create this page and store the response, which is the ID of the created page
                    $new_post_id = wp_insert_post($child_page_arguments);
                    // retrieve the parent post's language arguments
                    $original_post_language = apply_filters(
                        "wpml_element_language_details",
                        null,
                        [
                            "element_id"   => $post_id,
                            "element_type" => "issues",
                        ]
                    );
                    // retrieve WPML version of the post type
                    $wpml_element_type = apply_filters("wpml_element_type", "issues");
                    // set language arguments on the created child post
                    do_action(
                        "wpml_set_element_language_details",
                        [
                            "element_id"           => $new_post_id,
                            "element_type"         => $wpml_element_type,
                            "trid"                 => $original_post_language->trid,
                            "language_code"        => $original_post_language->language_code,
                            "source_language_code" => $original_post_language->language_code,
                        ]
                    );

                }
            }
        }
    }
}
add_action("save_post", "add_issue_child_posts");


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source