0

After migrating my posts to a new wordpress, I realized I needed featured images with my new custom theme.

function auto_featured_image() {
    global $post;

    if (!has_post_thumbnail($post->ID)) {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );

      if ($attached_image) {
              foreach ($attached_image as $attachment_id => $attachment) {
                   set_post_thumbnail($post->ID, $attachment_id);
              }
         }
    }
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');

This is the first result in google search but I can see the problem here being attachment. My posts have galleries as this [gallery ids="10271,10272,10273,10274,10275"] and these images display fine but they are not attached. They don't show up in "uploaded to this post" in media library.

I tried over 10 plugins for auto set first image as featured but none work as all of them requires attachment.

This code here actually looks for post contents but it looks for img tags I think.

// automatically retrieve the first image from posts
function get_first_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all( '/<img .+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
    $first_img = $matches[1][0];
    if ( empty( $first_img ) ) {
        // defines a fallback imaage
        $first_img = get_template_directory_uri() . "/images/default.jpg";
    }
    $first_img = '<img src="' . $first_img . '" alt="Post Image" />';
    return $first_img;
}

I can't say I understand much but if I can make this one look for "gallery ids" and merge with the above, it should work for what I want?

Here is my first php attempt:

// automatically retrieve the first image id from posts
function auto_featured_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match( '[gallery ids="(*?)"]', $post->post_content, $matches );
    $first_id = explode(",",$output[1]);
    set_post_thumbnail($post->ID, $first_id);
}

// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');

But it does nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.