Random Featured Image?

[ad_1]

Hej,

I need to develop a solution for which I need to generate multiple featured images for around 100 posts that do not have one. The featured image should be pulled from the post, and it should be the first image that the post content include.

Right now, the plugins I use make me end up with multiple random images that I NEVER uploaded to the site? It's a complete mess.

Any solutions?

[ad_2]
2 Comments
  1. The first image that you mentioned, does it happen to be posted within a specific div container everytime on every post? If so, you can write a javascript or php to read that specific class, search for<img> tag and pull src from them as a save in particular public_html directory. Also at the same time, save url for the given post from which image gets pulled; and update it’s profile image using a batch script by cross referencing the two saved details.

  2. you can run the following code once to set featured image that don’t have form post content

    function set_featured_image_from_first_post_image() {
    // Query for all posts without a featured image
    $args = array(
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘_thumbnail_id’,
    ‘compare’ => ‘NOT EXISTS’,
    ),
    ),
    );

    $posts = get_posts($args);

    foreach ($posts as $post) {
    // Get the post content
    $post_content = $post->post_content;

    // Search for the first image in the post content
    preg_match(‘/<img[^>]+src=[‘”]([^'”]+)[‘”]/i’, $post_content, $matches);

    if (isset($matches[1])) {
    // The URL of the first image
    $image_url = $matches[1];

    // Check if the image is already in the media library
    $image_id = attachment_url_to_postid($image_url);

    // If the image is not in the media library, upload it
    if (!$image_id) {
    $image_id = upload_image_from_url($image_url);
    }

    // Set the image as the featured image
    if ($image_id) {
    set_post_thumbnail($post->ID, $image_id);
    }
    }
    }
    }

    add_action(‘init’, ‘set_featured_image_from_first_post_image’, 20);

    // Function to upload image from URL
    function upload_image_from_url($image_url) {
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);

    if (wp_mkdir_p($upload_dir[‘path’])) {
    $file = $upload_dir[‘path’] . ‘/’ . $filename;
    } else {
    $file = $upload_dir[‘basedir’] . ‘/’ . $filename;
    }

    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => sanitize_file_name($filename),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’,
    );

    $attach_id = wp_insert_attachment($attachment, $file);
    require_once(ABSPATH . ‘wp-admin/includes/image.php’);
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    wp_update_attachment_metadata($attach_id, $attach_data);

    return $attach_id;
    }

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer