WordPress Popular Posts
How to retrieve most viewed posts using a custom select query ? (1 post)

  1. Cedric
    Posted 5 years ago #


    I use the WordPress Popular Post plugin and in order to display the most viewed posts as I want, I would like to grab them using a custom select query.

    I'm trying to achieve this with this code but unfortunately, it doesn't return any results:

    global $wpdb;
    global $post;
    $querystr = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->popularpostsdata.pageviews AS pageviews FROM $wpdb->posts LEFT JOIN $wpdb->popularpostsdata ON $wpdb->posts.ID = $wpdb->popularpostsdata.postid WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND $wpdb->popularpostsdata.pageviews > 0 AND $wpdb->posts.post_type = 'post' GROUP BY $wpdb->posts.ID ORDER BY pageviews DESC";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    <?php if ( $pageposts ): ?>
    <?php foreach( $pageposts as $post ) : ?>
    <?php setup_postdata($post); ?>
    <div class="ft_article">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <p><?php the_content(); ?></p>
    <?php endforeach; ?>
    <?php else : ?>
    <div class="ft_article">
    <p>No data available.</p>
    <?php endif; ?>

    Any help would be greatly appreciated !


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WordPress Popular Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic