Forums

exclude posts and home page from stats_get_csv (2 posts)

  1. mattoki
    Member
    Posted 6 months ago #

    Quick intro: I am a new WordPress user (2 months) and spent quite a bit of time looking for a way to show "most viewed/popular posts" in my archive section. After quite a bit of searching I stumbled upon a function included in Jetpack called stats_get_csv which helped me with this. The problem is that it showed both posts, PAGES AND HOME PAGE.

    I'm posting this to see if anyone has suggestions on alternatives or if anyone else can find this helpful in their own project. This particular code gets all your posts since inception and limits it to show the 2 most popular, excluding pages and the home page:

    <h4 class="top">Most Popular</h4>
    <?php $postcount = 0; ?>
    
    	<?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=-1&limit=-1') ) : ?>
    	<?php foreach ( $top_posts as $p ) : ?>
    		<?php $id = $p['post_id']; ?>
    		<?php $posttype = get_post_type($id); ?>
    			<?php if ( $posttype == "post" && $id !== "0") { ?>
    
    			<?php $postcount++; ?>
    
    			<!-- How you display your posts here -->
    
    		<?php } else if ($postcount == 3) { ?>
    		<?php break; ?>
    		<?php }; ?>
    
    <?php endforeach; ?>
    <?php endif; ?>
  2. mattoki
    Member
    Posted 5 months ago #

    Sorry, I realize now that I had my code wrong.

    <h4 class="top">Most Popular</h4>
    	<?php $postcount = 0; ?>
    <?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=90&limit=20') ) : ?>
    		<?php $postcount = 0; ?>
    <?php foreach ( $top_posts as $p ) : ?>
    	<?php $id = $p['post_id']; ?>
    	<?php $posttype = get_post_type($id); ?>
    	<?php $posttitle = get_the_title($id); ?>
    		<?php if ($postcount == 2) { ?>
    		<?php break; ?>
    		<?php } else if ( $posttype == "post" && $id !== "0") { ?>
    			<?php $postcount++; ?>
    
         <!-- enter your post display code here -->
    
    		<?php }; ?>
    	<?php endforeach; ?>
    <?php endif; ?>

Reply

You must log in to post.

About this Topic