Title: Loop: Most viewed posts doesn&#039;t work &#8211; why?
Last modified: August 20, 2016

---

# Loop: Most viewed posts doesn't work – why?

 *  [DjMG](https://wordpress.org/support/users/djmg/)
 * (@djmg)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/loop-most-viewed-posts-doesnt-work-why/)
 * Hi guys,
 * for displaying the most viewed posts i did install the plugin “wordpress popular
   posts”. With its new tables in the database, i can figure out, which post is 
   most viewed. The SQL statement, DOES work already (tested in phpmyadmin) like
   this:
 *     ```
       SELECT p. *
       FROM wp_posts AS p
       LEFT JOIN wp_popularpostsdata AS d ON p.ID = d.postid
       ORDER BY d.pageviews DESC
       ```
   
 * But when implementing it into the blogroll (category.php), it just gives me the
   newest entries of my site … why is that?
    Where is the mistake?
 *     ```
       <?php
       						$popular_posts = new WP_Query('SELECT p.* FROM wp_posts AS p LEFT JOIN wp_popularpostsdata AS d ON p.ID = d.postid ORDER BY d.pageviews DESC');
       						if($popular_posts->have_posts()): ?>
       							<?php while($popular_posts->have_posts()): $popular_posts->the_post(); ?>
       							<div class="technik-item-small">
       								<?php if(has_post_thumbnail()): ?>
       								<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'widget-image-thumb'); ?>
       								<div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>"  width='90' height='90' /></a><?php echo $icon; ?></div>
       								<?php else: ?>
       								<div class="block-image"><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.png&w=90&h=90" alt="<?php the_title(); ?>"  width='90' height='90' /></a><?php echo $icon; ?></div>
       								<?php endif; ?>
       								<h2><a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a></h2>
       							</div>
       							<?php endwhile; ?>
       						<?php endif; ?>
       ```
   
 * Sorry for my bad english 🙂
    Kind regards, DjMG

The topic ‘Loop: Most viewed posts doesn't work – why?’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [DjMG](https://wordpress.org/support/users/djmg/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/loop-most-viewed-posts-doesnt-work-why/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
