WordPress: Get Featured Image URL from Database

How I can get URL of Featured Image from database? I will show Featured Image in front end as .

Related posts

3 comments

  1. The featured image is stored in the

    wp_postmeta table with the meta_key _thumbnail_id

    you can get it by

    $Featured_image = $wpdb->get_results("
    SELECT p.*
      FROM net_5_postmeta AS pm
     INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID 
     WHERE pm.post_id = $da_id
       AND pm.meta_key = '_thumbnail_id' 
     ORDER BY p.post_date DESC 
     LIMIT 15
     ",'ARRAY_A'
    

    or

    SELECT * from {$wpdb->prefix}_posts 
    WHERE ID in (
    SELECT meta_value FROM {$wpdb->prefix}_postmeta 
    WHERE meta_key = '_thumbnail_id'
    AND post_id = ':ID'
    );
    

    Replace ID by your post id

    To Get the Post Thumbnail URL in WordPress

    <?php
    $thumb_id = get_post_thumbnail_id();
    $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
    echo $thumb_url[0];
    ?>
    

    for reference :URL

  2. You can try this code

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
               if ( has_post_thumbnail() ) {
            $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
                   // use the $feat_image_url variable as you like
               }
               endwhile;
             endif;
    

    Hope this helps

    Take care and happy coding

  3. Please Try This

    <?php $query = new WP_Query($args); ?>
    <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
    
    <?php if (has_post_thumbnail()): ?>
        <a class="feature_image" href="<?php echo wp_get_attachment_url( get_post_thumbnail_id(get_the_ID())); ?>">
            <?php the_post_thumbnail('thumbnail'); ?>
        </a>
    <?php endif; ?>
    

Comments are closed.