I retrieve information from database postmeta table. I have 2 custom post type 'book' and 'author' I using metabox to connect them together. when user go to book custom post and adding new book. he must use check box metabox to determined which author write this book.

I have another page in my website which show author profile. in this page user can see books of author, my codes can save author for each book in database and read them perfectly and also can retrieve books of any author.

here is my problem I want to find each book feature image but when I use get_the_post_thumbnail it doesn't give me anything.

how can I fix this

I use var_dump to see each book information here is the picture of it. enter image description here

and here is my codes

<?php $args = array( 'post_type' => 'author'); 
    $loop = new WP_Query( $args );
     while ( have_posts() ) : the_post(); ?>

    <div class="title-pack col-md-12 col-sm-12 col-xs-12">
        <span class="line visible-sm-block"></span>
        <span class="visible-sm-block tittle-style"><?php the_title(); ?></span>
    <div class="row writer-crit">
        <div class="writer-crit-box col-md-9 col-sm-8 col-xs-12">
            <div class="col-md-11 col-sm-11 col-xs-12 pull-right">
                <div class="writer-bio pull-right col-md-12 col-sm-12 col-xs-12">
                    <?php the_post_thumbnail('post-thumbnail',array('class' => 'pull-right')); ?>
                    <div class="writer-content-bio col-md-8 col-sm-8 col-xs-12 pull-right">
                        <h3><?php the_title(); ?></h3>
                        <?php the_content(); ?>
                    <div class="col-md-12 col-sm-12 col-xs-12 pull-right">
                            <h3>کتابشناسی </h3>
                            <?php $ars = array( 'post_type' => 'book'); 
                            $loop = new WP_Query( $ars );

// for reading author which choose from cheak box in each book pages.
                            $post_id = get_the_ID();

                            $key = 'save-author-to-book';
                            // $vals=get_post_meta($post_id, $key2, true);
                            // $values = get_post_meta( $post_id, $key, true );
                            $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                            global $wpdb;
                            $x=(string) $post_id;
                            $sql='SELECT post_id FROM wp_postmeta WHERE meta_key = "save-author-to-book" AND meta_value LIKE "%'.$x.'%"';
                            $results = $wpdb->get_results( $sql, OBJECT );
                            foreach ($results as $result ) {

                            foreach ($array as $arr) { ?>
                                <?php $autr_book=get_post($arr);

                                <li class="">
                                    <a href="<?php echo $autr_book->guid;?>">
                                    <div><?php get_the_post_thumbnail( $autr_book->ID ); ?></div>
                                <p><?php echo $autr_book->post_title; ?></p>
                            <?php } ?>


<?php endwhile; // End of the loop. ?>
<!-- ====================================
  • get_the_post_thumbnail returns a string. You need to echo it.
    – ngearing
    Sep 1, 2016 at 4:25
  • Did you notice that you are getting POST ID as $post_id = get_the_ID(); but later you are trying to get thumbnail with $post->ID. So try this: $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post_id) ); and var_dump($feat_image); Sep 1, 2016 at 4:27
  • @Nath OH, your right. I completely forget this. please sent your answer. thank you.
    – mkafiyan
    Sep 1, 2016 at 4:27
  • @WordPressMechanic yes your right too. this is the first wordpress project and full of flaw :(
    – mkafiyan
    Sep 1, 2016 at 4:29

2 Answers 2


You use the function get_the_post_thumbnail() which returns a string. So you need to print that string with echo or use the_post_thumbnail() which echos itself.

  • sorry but another question about this. in custom post type book I have custom filed name cust but it doesn't retrieve when I read from db. I used something like this <h4><?php echo the_field($autr_book->cost); ?></h4> but it doesn't work. how can I solve this?
    – mkafiyan
    Sep 1, 2016 at 4:46
  • @mimi the_field() can use two parameters the_field($field_name, $post_id), so try something like the_field('cost', $autr_book->ID).
    – ngearing
    Sep 1, 2016 at 4:55
  • @mimi again with the echoing function vs non echoing function, you can do echo get_field() or the_field()
    – ngearing
    Sep 1, 2016 at 4:56

Have a look here:


$post_thumbnail_id = get_post_thumbnail_id( $post_id );

You are almost there if you correct your $post_id variable.

Problem is here: $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );


$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post_id) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.