Magento Engineer
social
Journal

Create wprdpress posts by csv with featured image by code.

<?php
// Include WordPress bootstrap file
require_once 'wp-load.php';

// Path to your CSV file
$csv_file = '1.csv';

// Function to download and attach the image to the post
function attach_featured_image($post_id, $image_url) {
    // Download the image from the URL
    $image_contents = file_get_contents($image_url);
    
    if ($image_contents !== false) {
        // Get the filename from the URL
        $filename = basename($image_url);
        
        // Upload the image to the WordPress media library
        $upload = wp_upload_bits($filename, null, $image_contents);
        
        if (!$upload['error']) {
            // Prepare the attachment data
            $attachment_data = array(
                'post_mime_type' => $upload['type'],
                'post_parent' => $post_id,
                'post_title' => sanitize_file_name(pathinfo($filename, PATHINFO_FILENAME)),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            // Insert the attachment into the database
            $attach_id = wp_insert_attachment($attachment_data, $upload['file']);
            // Set the featured image
            if (!is_wp_error($attach_id)) {
                set_post_thumbnail($post_id, $attach_id);
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}




// Open the CSV file for reading
if (($handle = fopen($csv_file, 'r')) !== false) {
    // Loop through each row in the CSV file
    while (($data = fgetcsv($handle, 1000, ',')) !== false) {
        // Extract data from CSV row
        $post_title    = $data[0]; // Assuming first column is post title        
        $custom_field1 = $data[1]; // Assuming fourth column is custom field 1 value
        $custom_field2 = $data[2]; // Assuming fifth column is custom field 2 value
        $custom_field3 = $data[3]; // Assuming fifth column is custom field 2 value
        $custom_field4 = $data[4]; // Assuming fifth column is custom field 2 value
        $image_url     = $data[5]; // Assuming third column is image URL
        $post_content  = $data[6]; // Assuming second column is post content        
        
        // Add more custom fields as needed
        //title   seo_title   seo_description keyword published_at    featured_image  content

        // Create post array with custom fields

        $existing_post = get_page_by_title($post_title, OBJECT, 'post');

        if ($existing_post) {
                    // Post already exists, update the post content and featured image
                    $post_id = $existing_post->ID;
                    $post_data = array(
                        'ID'           => $post_id,
                        'post_content' => $post_content,
                    );
                    wp_update_post($post_data);

                    // Attach featured image if URL is provided
                    if (!empty($image_url) && !is_wp_error($post_id)) {
                        attach_featured_image($post_id, $image_url);
                    }
                    
                    echo "Post updated: $post_title<br>";
                } else {

                    $new_post = array(
                        'post_title'   => $post_title,
                        'post_content' => $post_content,
                        // Add more fields as needed
                        'post_status'  => 'publish', // You can change post status if needed
                        'post_author'  => 1, // Change the author ID if needed
                        'post_type'    => 'post', // Change the post type if needed
                        'meta_input'   => array(
                            'seo_title'       => $custom_field1,
                            'seo_description' => $custom_field2,
                            'keyword'         => $custom_field3,
                            'published_at'    => $custom_field4,
                            'featured_img'    => $image_url,
                            // Add more custom fields as needed
                        ),
                    );
                }

        // Insert the post into the database
        $post_id = wp_insert_post($new_post);

        // Attach featured image if URL is provided
        if (!empty($image_url) && !is_wp_error($post_id)) {
            attach_featured_image($post_id, $image_url);
        }
        // Handle errors if any
        if (is_wp_error($post_id)) {
            $errors = $post_id->get_error_messages();
            foreach ($errors as $error) {
                echo $error . '<br>';
            }
        }
    }
    // Close the CSV file
    echo "CSV uploaded successfully!";
    fclose($handle);
} else {
    echo 'Error opening CSV file';
}
?>

Ad comes here
Comments
Leave A Comment
PHP blog
Update Cookies Preferences