WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. cooperanet
    Member
    Posted 3 years ago #

    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/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic