• Hi,

    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:

    <?php
    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>
    </div>
    <?php endforeach; ?>
    <?php else : ?>
    <div class="ft_article">
    <p>No data available.</p>
    </div>
    <?php endif; ?>

    Any help would be greatly appreciated !

    http://wordpress.org/extend/plugins/wordpress-popular-posts/

  • The topic ‘[Plugin: WordPress Popular Posts] How to retrieve most viewed posts using a custom select query ?’ is closed to new replies.