Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to know how it's possible to get the 10 most viewed pages ? I know how make it with post, but not with page...

Thanks for your help.

share|improve this question
    
just add the code you use in page template. Pages are posts. –  JMau Jul 1 '13 at 8:56
    
This doesn't work : <?php query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC')‌​; while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" ><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> That is my code and it just get me the post (article); Thanks for your fast answer ;) –  PixelBBW Jul 1 '13 at 9:12

1 Answer 1

If you want to query pages you have to choose post_type=page - of course.

Another "problem" is that the commen count isn't exactly representing post/page views. If you really want page views - not comment count - try something like the function below - I got that from here: http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/.

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

With that you would have to query for the new meta key post_views_count. That's all - good luck!

share|improve this answer
1  
Just remember that counting views in this way ins't very good idea if your site is popular. It will update your database every time someone visits any page of your site. It's a lot of update queries. –  Krzysiek Dróżdż Jul 1 '13 at 12:13
    
@KrzysiekDróżdż: That's true, I probably should mentioned that. Do you have a more efficient way in mind? For actual page views. And if so, could you share it? –  ialocin Jul 1 '13 at 12:25
    
I'm afraid I don't :( Well, you could use Google Analytics API to do it (I guess), but it definitely is more complex solution (and assumes you use GA). –  Krzysiek Dróżdż Jul 1 '13 at 12:27
    
Too bad, that would be interesting. I guess Google Analytics is a way to do it, but personally I only use it if really necessary, so, a internal solution is preferable - for me at least. To summarize if you are planing to use a solution working with post meta make sure your server plan has the scale to do so without slowing your site down to much. –  ialocin Jul 1 '13 at 12:41
    
Well, doing stats yourself on your server is generally a bad idea - it will always use a lot of resources... –  Krzysiek Dróżdż Jul 1 '13 at 12:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.