0

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);
        }
    }
}
6
  • Have you debug what type of error it shows? Commented Aug 24, 2021 at 7:35
  • No its not showing any error, I enabled wp_debug and it just won't run
    – Ajay Malik
    Commented Aug 24, 2021 at 7:39
  • I tried this one also gist.github.com/royduin/69ff94b68c28419dbd9a8e07dcf13187
    – Ajay Malik
    Commented Aug 24, 2021 at 7:40
  • Does your XML file use image or guid. If its guid then replace this line media_sideload_image($item->image, $postInsertId, $item->title); accordingly.
    – Rodgath
    Commented Aug 24, 2021 at 9:43
  • Another thing, move your featuredImageTrick function outside the importIt function to avoid getting undefined function error.
    – Rodgath
    Commented Aug 24, 2021 at 9:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.