Upload images: Put all the images in a folder on your server and the image file names are exactly what you want the post titles to be.
Create a Custom PHP Script: Create a custom PHP script that uploads the images and assigns them as featured images to new posts.
Version 1:
<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load WordPress
require_once(dirname(__FILE__) . '/../../wp-load.php'); // Adjusted path
// Check if accessed directly
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
// Set the directory where your images are stored
$image_dir = ABSPATH . 'wp-content/uploads/bulkimage/';
$images = scandir($image_dir);
if (empty($images)) {
echo "No images found in directory: $image_dir<br>";
exit;
}
foreach ($images as $image) {
// Skip . and ..
if ($image === '.' || $image === '..') continue;
// Get the title from the image name (without extension)
$title = pathinfo($image, PATHINFO_FILENAME);
// Prepare post data
$post_data = array(
'post_title' => $title,
'post_content' => '', // Add content if needed
'post_status' => 'publish',
'post_type' => 'theme_portfolio', // Change to your desired post type
);
// Insert the post into the database
$post_id = wp_insert_post($post_data);
// Debug: Check if post was created successfully
if (is_wp_error($post_id)) {
echo "Error creating post for image: $image. Error: " . $post_id->get_error_message() . "<br>";
continue; // Skip to the next image
}
echo "Post created successfully: ID $post_id for image $image<br>";
// Prepare the image attachment
$file = $image_dir . $image;
// Check if the file exists before inserting
if (file_exists($file)) {
// Include the WordPress file upload handling
$attach_id = wp_insert_attachment(array(
'guid' => wp_upload_dir()['url'] . '/' . $image, // Create the GUID
'post_mime_type' => wp_check_filetype(basename($file), null)['type'],
'post_title' => $title,
'post_content' => '',
'post_status' => 'inherit'
), $file, $post_id);
if (is_wp_error($attach_id)) {
echo "Error inserting attachment for post ID $post_id. Error: " . $attach_id->get_error_message() . "<br>";
continue; // Skip to the next image
}
echo "Attachment created successfully: ID $attach_id for post ID $post_id<br>";
// Include image.php to handle image processing
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate attachment metadata and update the database record
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
} else {
echo "File not found: $file<br>";
}
}
echo "Upload complete!";
?>
Upload the Script: Save this script as upload-images.php or something similar.
Place the script in the root directory of your WordPress installation or wherever you can access WordPress core functions.
Run the Script: Visit the script URL (e.g., http://yourdomain.com/upload-images.php), and it will automatically create posts with the image names as titles and set the corresponding image as the featured image.
";
exit;
}
foreach ($images as $image) {
// Skip . and ..
if ($image === '.' || $image === '..') continue;
// Get the title from the image name (without extension)
$title = pathinfo($image, PATHINFO_FILENAME);
// Prepare post data
$post_data = array(
'post_title' => $title,
'post_content' => '', // Add content if needed
'post_status' => 'publish',
'post_type' => 'theme_portfolio', // Change to your desired post type
);
// Insert the post into the database
$post_id = wp_insert_post($post_data);
// Debug: Check if post was created successfully
if (is_wp_error($post_id)) {
echo "Error creating post for image: $image. Error: " . $post_id->get_error_message() . "
";
continue; // Skip to the next image
}
echo "Post created successfully: ID $post_id for image $image
";
// Prepare the image attachment
$file = $image_dir . $image;
// Check if the file exists before inserting
if (file_exists($file)) {
// Include the WordPress file upload handling
$attach_id = wp_insert_attachment(array(
'guid' => wp_upload_dir()['url'] . '/' . $image, // Create the GUID
'post_mime_type' => wp_check_filetype(basename($file), null)['type'],
'post_title' => $title,
'post_content' => '',
'post_status' => 'inherit'
), $file, $post_id);
if (is_wp_error($attach_id)) {
echo "Error inserting attachment for post ID $post_id. Error: " . $attach_id->get_error_message() . "
";
continue; // Skip to the next image
}
echo "Attachment created successfully: ID $attach_id for post ID $post_id
";
// Include image.php to handle image processing
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate attachment metadata and update the database record
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
} else {
echo "File not found: $file
";
}
}
echo "Upload complete!";
?>