plugins – wp_insert_post not inserting post from XML

Question

I’m using below to read XML file and import its content as a page in wordpress, but it’s not working:

add_action('import_demo', 'importIt');
function importIt(){
    // The code until now
    $path = get_template_directory() . '/demo/pages.xml';
    $xml = simplexml_load_file($path);
    foreach ($xml->channel->item as $item) {
        $postCreated = array(
            'post_title'    => $item->title,
            'post_content'  => $item->content,
            // 'post_excerpt'  => $item->excerpt,
            'post_status'   => 'publish',
            'post_type'     => 'page', // Or "page" or some custom post type
        );

        $postInsertId = wp_insert_post( $postCreated );

        add_action('add_attachment','featuredImageTrick');
        media_sideload_image($item->image, $postInsertId, $item->title);
        remove_action('add_attachment','featuredImageTrick');

        function featuredImageTrick($att_id){
            $p = get_post($att_id);

            update_post_meta($p->post_parent,'_thumbnail_id',$att_id);
        }
    }
}

0
Rup 4 weeks 2021-08-24T02:13:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse