Title: Problem with custom function (most read posts)
Last modified: August 20, 2016

---

# Problem with custom function (most read posts)

 *  [onirisweb](https://wordpress.org/support/users/onirisweb/)
 * (@onirisweb)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-with-custom-function-most-read-posts/)
 * Hi,
 * I created a custom function to display the most read articles in my sidebar.
 * Here is the code :
 *     ```
       function popularPosts($num) {
           global $wpdb;
   
           $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
   
           foreach ($posts as $post) {
               setup_postdata($post);
               $id = $post->ID;
               $title = $post->post_title;
               $count = $post->comment_count;
       	   if ( has_post_thumbnail( $post->ID ) ){ $content = '<p>' . get_the_post_thumbnail( $post->ID, 'thumbnail'
       ) . '</p>'; }
       else {
       $content = '<p><img src="http://onirisweb.net/wp-content/uploads/2012/11/no-thumb.jpg"/></p>';
       }
   
               if ($count != 0) {
                   $popular .= '<li>';
       	    $popular .=  $content;
                   $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';
                   $popular .= '</li>';
               }
           }
           return $popular;
       }
       ```
   
 * You can see the demo [here](http://onirisweb.net/qui-tient-la-plume-du-blog/)
 * Normally I can display the featured images, but when there isn’t, then the default
   thumb must be displayed (question mark).
 * As you can see, my problem is that some titles which don’t have featured images
   don’t display question mark thumb.
 * Anyone could help me ?
 * Thanks a lot

The topic ‘Problem with custom function (most read posts)’ is closed to new replies.

## Tags

 * [featured image](https://wordpress.org/support/topic-tag/featured-image/)
 * [function](https://wordpress.org/support/topic-tag/function/)

 * 0 replies
 * 1 participant
 * Last reply from: [onirisweb](https://wordpress.org/support/users/onirisweb/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/problem-with-custom-function-most-read-posts/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
